DOM Selectors
This lesson explains DOM Selectors in JavaScript with beginner-friendly examples, practical use cases, and clear best practices.
JavaScript DOM Selectors Overview
JavaScript DOM selectors are built-in methods that locate and retrieve HTML
elements from a web page. They allow developers to read, update, style, and
manipulate specific elements dynamically.
DOM selectors are one of the most frequently used JavaScript features in
frontend development. They are essential for event handling, form
validation, interactive user interfaces, animations, dashboards, and
single-page applications built with React, Angular, Vue, and vanilla
JavaScript.
| Selector | Description | Returns |
| getElementById() | Selects an element by ID. | Single element |
| getElementsByClassName() | Selects elements by class. | HTMLCollection |
| getElementsByTagName() | Selects elements by tag. | HTMLCollection |
| querySelector() | Selects the first matching CSS selector. | Single element |
| querySelectorAll() | Selects all matching CSS selectors. | NodeList |
JavaScript DOM Selectors Example
const heading = document.getElementById("title");
const button = document.querySelector(".btn");
const cards = document.querySelectorAll(".card");
heading.textContent = "Welcome to JavaScript";
button.addEventListener("click", function() {
console.log(cards.length);
});
This example demonstrates the most common DOM selectors used to select
elements by ID, class, and CSS selector before updating content or handling
user interactions.
Top 10 JavaScript DOM Selector Examples
These are the most commonly used DOM selectors in modern JavaScript
applications and frontend interviews.
| # | Example | Syntax |
| 1 | Select by ID | document.getElementById("title") |
| 2 | Select by Class | document.getElementsByClassName("card") |
| 3 | Select by Tag | document.getElementsByTagName("p") |
| 4 | Select First CSS Match | document.querySelector(".btn") |
| 5 | Select All CSS Matches | document.querySelectorAll(".card") |
| 6 | Select by Attribute | document.querySelector("[data-id]") |
| 7 | Select Checked Checkbox | document.querySelector(":checked") |
| 8 | Select First Child | document.querySelector("li:first-child") |
| 9 | Select Last Child | document.querySelector("li:last-child") |
| 10 | Select Nested Element | document.querySelector(".menu li a") |
Popular Real-World DOM Selector Examples
| Scenario | Selector |
| Login Button | document.querySelector("#loginButton") |
| Search Input | document.querySelector(".search-box") |
| Navigation Links | document.querySelectorAll(".nav-link") |
| Selected Radio Button | document.querySelector("input:checked") |
| All Form Fields | document.querySelectorAll("input") |
| Table Rows | document.querySelectorAll("tbody tr") |
| Modal Window | document.querySelector(".modal") |
| Dropdown Menu | document.querySelector("#country") |
| Error Message | document.querySelector(".error") |
| Active Menu Item | document.querySelector(".active") |
Best Practices
- Prefer
querySelector() and querySelectorAll() for modern JavaScript development. - Use IDs only when elements are unique.
- Use descriptive CSS classes instead of deeply nested selectors.
- Cache frequently accessed DOM elements.
- Check that an element exists before updating it.
- Use
querySelectorAll() when working with multiple matching elements. - Avoid repeatedly searching the DOM inside loops.
- Keep selectors simple for better performance and readability.
Common Mistakes
- Using
getElementById() with a CSS selector. - Expecting
querySelectorAll() to return a single element. - Confusing
HTMLCollection with NodeList. - Selecting elements before the DOM has loaded.
- Using overly complex CSS selectors.
- Ignoring null checks after selecting elements.
- Searching the DOM repeatedly instead of storing references.
Key Takeaways
- DOM selectors locate HTML elements for manipulation.
querySelector() is the most commonly used selector in modern JavaScript. querySelectorAll() returns all matching elements. getElementById() is the fastest way to select an element by ID. - Choose the selector based on your use case and readability.
- Efficient DOM selection improves application performance.
Pro Tip
In modern JavaScript, querySelector() and
querySelectorAll() are the preferred DOM selectors because
they support the full power of CSS selectors and work consistently across
modern browsers.