Find HTML DOM Elements in JavaScript
In this post, I will introduce several ways to find HTML DOM elements in JavaScript.
querySelector methods
Document or Element methods
querySelectorAll(selectors)
- Parameters: the selectors parameter is a valid CSS selector string.
- Return Value: returns a static (not live)
NodeListrepresenting a list of the document’s elements that match the specified group of selectors, or an emptyNodeListin case of no matches.
querySelector(selectors)
- Parameters: the selectors parameter is a valid CSS selector string.
- Return Value: return an
Elementobject representing the first element in the document that matches the specified set of CSS selectors, ornullis returned if there are no matches.
Find by id
document.querySelector("#test") |
Find by HTML tag
document.querySelectorAll("p") |
Find by class names
document.querySelectorAll("div.className1") |
Find by attributes
// match elements contain a attribute |
and
document.querySelectorAll("div.className1.className2"); |
document.querySelectorAll("div[attr1='value1'][attr2='value2']"); |
or
document.querySelectorAll("div.className1, div.className2"); |
Find descendants, children, siblings with tag names
// descendants |
:nth-child(n): Selects thenth child of a parent, regardless of the type of element.:first-child:last-child:nth-of-type(n): Selects thenth child of a specific type within its parent.:first-of-type:last-of-type
For more information about CSS selectors, you can see the CSS selectors documentation.
Examples
// Find the second <td> with a specific style in a <table> |
Traversing match elements
const matchedElements = document.querySelectorAll("div"); |
Filter elements
const matchedElements = document.querySelectorAll("div"); |
const matchedElements = document.querySelectorAll("div"); |
getElementsBy methods
Document or Element methods
getElementById(id)
- Return value: An
Elementobject describing the DOM element object matching the specified ID, ornullif no matching element was found in the document.
getElementsByClassName(className)
- Parameters:
classNamea string representing the class name(s) to match; multiple class names are separated by whitespace. - Return value: A live
HTMLCollectionof found elements.
getElementsByName(nameAttrValue)
- Parameters:
nameAttrValuethe value of thenameattribute of the element(s) we are looking for. - Return value: A live
NodeListcollection, meaning it automatically updates as new elements with the samenameare added to, or removed from, the document.
getElementsByTagName(elementName)
- Parameters:
elementNamea string representing the name of the elements. The special string*represents all elements. - Return value: A live
HTMLCollectionof found elements.
Traverse elements
const testElements = document.getElementsByClassName('test'); |
Filter elements
const matchedElements = document.getElementsByTagName("div"); |
const matchedElements = document.getElementsByTagName("div"); |
The closest method
closest(selector) looks for the nearest ancestor that matches the CSS-selector.
Summary
| Method | Searches by | Call on an element | Live |
|---|---|---|---|
querySelector |
CSS-selector | Yes | - |
querySelectorAll |
CSS-selector | Yes | - |
getElementById |
id |
- | - |
getElementsByName |
name |
- | Yes |
getElementsByTagName |
tag or '*' |
Yes | Yes |
getElementsByClassName |
class | Yes | Yes |
References
[1] Searching: getElement*, querySelector*
[2] Document.querySelector() - mdn
[3] Document.querySelectorAll() - mdn