The querySelectorAll()
method returns all elements from a DOM tree with the same selector.
The method is called on the parent node document
.
The parameter for the function is the value of the class
property.
This function returns all the elements with the specified class
as an array.
The following code selects all elements with the class
value demo
and updates the style of the first element only:
div
element with two child elements.p
element with the class demo
.p
element with the same class demo
.onClick
button is hooked to the function change()
.change()
function in the script tag.querySelectorAll()
on the document
and pass a CSS selector name. Here, it is the demo
class. It returns all the elements with the same selector. elem
is the list of elements with selector demo
.style.color
to elem[0]
, which is the first element with the demo
selector.