theory.js

The DOM & Event Handling

Selecting Elements

JavaScript allows you to select elements in the DOM to manipulate them.

  • `document.querySelector()` selects the first matching element.
  • `document.querySelectorAll()` selects all matching elements.
  • `document.getElementById()` selects an element by its ID.
// Select an element by ID
let heading = document.getElementById("title");
console.log(heading.textContent);

// Select the first matching element
let button = document.querySelector(".btn");
console.log(button.textContent);