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
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.