Learn HTML DOM
Accessing DOM Elements
3.3
Lesson

Selecting DOM Elements Using Class Name

getElementsByClassName will select all the HTML elements which match the provided class name.

The method returns an array-like list of elements. So, we can access a specific element using the bracket notation, []. None of the array methods will work though.

In the above example, we get the list of all p elements with the class name inspirational-words, and then loop over it to apply changes to each one of them. You can see that the changes do not apply to the last p element, because it does not have the required class name.

If you want to use the array methods, you can convert the list to an array using the Array.from() method.