getElementsByClassName method
The getElementsByClassName method
allows you to get page elements by their
class specified in the
class
attribute. It will be possible to perform
various manipulations with the retreived
elements: change their text, attributes,
CSS styles, and so on.
Syntax
document.getElementsByClassName(class name);
Example
Let's get all elements with the class
www and change their text using the
innerHTML
property:
<p class="www">elem 1</p>
<p class="www">elem 2</p>
<p class="www">elem 3</p>
let elems = document.getElementsByClassName('www');
for (let elem of elems) {
elem.innerHTML = '!!!';
}
The code execution result:
<p class="www">!!!</p>
<p class="www">!!!</p>
<p class="www">!!!</p>
See also
-
the
querySelectorAllmethod
that gets a group of elements by a selector -
the
getElementsByTagNamemethod
that gets a group of elements by a tag