--- title: HTML DOM querySelector() --- The Document method `querySelector()` returns the `first` Element within the document that matches the specified selector, or group of selectors. If no matches are found,null is returned. **HTML content:** ```html
element-example ``` **JavaScript content:** ```javascript document.querySelector("#id-example"); // Returns the element with id "id-example" document.querySelector(".class-example"); // Returns the element with class "class-example" document.querySelector("a"); // Returns the "a" element ``` Note `querySelector()` returns the first matching element, to return all the matches, use the querySelectorAll() method instead. ```html
First
Second
``` ```javascript document.querySelector("#example"); // Returns only the element containing 'First' ``` #### More Information: MDN - document.querySelector()