Unit 3: Objects, DOM & Browser Web APIsLesson #7 / 10

Lesson 7: DOM Queries, document.querySelector & Traversal

Er. Manoj Kumar — AuthorEr. Manoj KumarLast Updated: 26 Aug, 2026

1. The Document Object Model (DOM)

The DOM is the tree-like API representation of HTML documents created by the browser during parsing.

  • document.getElementById('id'): Fast, direct lookup by ID.
  • document.querySelector('.class / #id / tag'): Selects the first matching CSS selector.
  • document.querySelectorAll('.item'): Selects all matching elements into an iterable NodeList.
  • HTML5 Web Code (index.html)
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>DOM Selection Demo</title>
    </head>
    <body style="background:#17181c; color:#fff; font-family:Arial; padding:30px;">
    <h2>DOM Node Selection</h2>
    <p class="highlight-item">Item 1</p>
    <p class="highlight-item">Item 2</p>
    <p class="highlight-item">Item 3</p>
    <script>
    // Select all items with class 'highlight-item'
    const items = document.querySelectorAll(".highlight-item");
    items.forEach((item, idx) => {
    item.style.backgroundColor = "#1f2029";
    item.style.padding = "8px 12px";
    item.style.borderLeft = "4px solid #04AA6D";
    item.style.margin = "6px 0";
    item.textContent = "✓ Verified Item #" + (idx + 1);
    });
    </script>
    </body>
    </html>

    Interactive Knowledge Check

    Test your understanding of Lesson #7 concepts

    Which query method returns ALL matching elements in the DOM as a NodeList?