getElementById and getElementsByClassName
Use classic selection methods.
getElementById
getElementById returns the single element with the matching id, or null. Ids must be unique on a page, so this returns one element.
const header = document.getElementById("header");
console.log(header.tagName);No Hash Symbol
Unlike CSS selectors, you pass the raw id without the #. Passing "#header" would fail to match.
document.getElementById("menu"); // correct
document.getElementById("#menu"); // wrong, returns nullAll lessons in this course
- Selecting Elements with querySelector
- getElementById and getElementsByClassName
- Traversing Parents, Children and Siblings
- NodeList vs HTMLCollection