How do you select elements in the DOM?

MethodReturns
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
Interview Questions & Answers
What is the DOM in JavaScript?
How do you select elements in the DOM?
What are events and event listeners in JavaScript?
What is event bubbling and capturing in JavaScript?
What is event delegation in JavaScript?