CHAPTER 5Element Queries
Get Elements By
Three document
methods let us search for elements in the 3 most typical ways:
getElementsByTagName(tag)
: (plural!) find all paragraphs, or allh1
s.getElementById(id)
: (singular!) find the element with a certainid
.getElementsByClassName(class)
: (plural!) find all elements with a certainclass
.
Query Selector
Two other methods let us search using CSS selectors, which are more powerful. This methods were introduced around 2008 to mirror the functionality that at the time provided a library called jQuery, which still exists:
querySelector(sel)
: Find the first element matching selectorsel
.querySelectorAll(sel)
: Find all elements matching selectorsel
.