DEV Community

Cover image for Selecting an Element
GiandoDev
GiandoDev

Posted on

Selecting an Element

In JavaScript if we want to select an element inside our DOM and for example we want to change the color, we can do it this way
Alt Text
The document object contains the method querySelector that allows us to point to the element that we want to
Alt Text
As in the example above we pass inside querySelector("ID / CLASS / TAG / and ATTRIBUTE but just [INSIDE SQUARE BRACKETS ]")
No matter how many elements match our call; will always be selected the first starting from the root element and down to the last element
Alt Text
There are other ways to select multiple items at once but we'll talk about it in another moment (querySelectorAll).
Alt Text
In the example above we see that every element has the querySelector method too so we may select an element children through its father without going through the root element and going down the whole tree.

Discussion (0)