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.

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay