How do you select elements in the DOM?
| Method | Returns |
|---|---|
getElementById("id") | One element |
querySelector("css") | First match of a CSS selector |
querySelectorAll("css") | All matches (NodeList) |
getElementsByClassName("c") | Live HTMLCollection of a class |
const a = document.getElementById("title");
const b = document.querySelector(".note");
const all = document.querySelectorAll("p.note");
querySelector and querySelectorAll are the most flexible, because they use CSS selectors. See HTML DOM.
Learn more about the similar topics:
| Tutorials |
|---|
| JavaScript Events |
| JavaScript HTML DOM |
| JavaScript DOM Manipulation |
| JavaScript HTML DOM EventListener |
| JavaScript Forms and Validation |
| Exercises & Assignments |
|---|
| Write a JavaScript program to change the text of a paragraph on button click |
| Write a JavaScript program to switch dark mode on and off |