JavaScript getElementsByClassName() Method Tutorial

In this section, we will learn what the Element getElementsByClassName() method is and how to use it in JavaScript.

What is Element getElementsByClassName() Method in JavaScript?

The JavaScript Element getElementsByClassName() method is used to find those elements in an HTML document that first, are the descendants of the element that invoked this method and second, contain a specific value for their class attribute.

Element getElementsByClassName() Method Syntax:

element.getElementsByClassName(“class-name”);

Element getElementsByClassName() Method Parameter

The method takes one argument and that is the name of the target class.

Note: using this method, you can search for more than just one class name! If that’s the case, separate each value using a white-space from the other class value.

Element getElementsByClassName() Method Return Value

The return value of this method is a collection object and each of its items is a reference to an element that matched the requirement of this method.

Example: document getElementsByClassName() method

See the Pen document getElementsByClassName() method by Omid Dehghan (@odchan1) on CodePen.

Example: document get element by class

See the Pen document get element by class by Omid Dehghan (@odchan1) on CodePen.

Leave a Reply