How to use JavaScript to get elements by class name?

In this short tutorial, we use JavaScript to get element by class name. We break down the concept, explain the code and then discuss the limitations of the method used.

This tutorial is a part of our initiative at Flexiple, to write short curated tuto…


This content originally appeared on DEV Community and was authored by hrishikesh1990

In this short tutorial, we use JavaScript to get element by class name. We break down the concept, explain the code and then discuss the limitations of the method used.

This tutorial is a part of our initiative at Flexiple, to write short curated tutorials around often used or interesting concepts.

Table of Contents - Javascript get element by class:

How does the class attribute work?

The class attribute is an optional property of an HTML element. This attribute can be used on any HTML element. Once a class is created their name can be used by CSS or JavaScript to apply a particular style or to perform certain tasks. This ensures that all the elements belonging to a particular class behave and appear in a similar fashion.
The below code is an example of a class attribute.

<h2 class="classname">Title</h2>

Using the JavaScript getElementByClassName() method:

The JavaScript getElementsByClassName is used to get all the elements that belong to a particular class. When the JavaScript get element by class name method is called on the document object, it searches the complete document, including the root nodes, and returns an array containing all the elements.

The syntax is as follows:

var elements = document.getElementsByClassName(name);

Here “name” is the class name you are looking to find and “elements” is a variable that would contain the array of elements.

Code and Explanation:

Let’s take a sample HTML code:

<header>
<nav>
        <ul id="freelancer">
            <li class="item">Name</li>
            <li class="item">Skills</li>
            <li class="item price">Cost</li>
            <li class="item">Projects</li>
        </ul>
</nav>
</header>

Now since we intend to look for the element in a particular section we first identify the section along with the ID.

let docs = document.getElementByID('#freelancer');
let elements = freelancer.getElementsByClassName('item');

Now ‘elements’ contain the list of elements with the class name ‘item’.

Limitation & Caveats - JavaScript get element by class:

When using the aforementioned method, note that
If you are trying to get elements with the different class names their names must be separated by whitespace and not a comma.
Class selectors cannot be used in this method.


This content originally appeared on DEV Community and was authored by hrishikesh1990


Print Share Comment Cite Upload Translate Updates
APA

hrishikesh1990 | Sciencx (2021-10-12T11:30:56+00:00) How to use JavaScript to get elements by class name?. Retrieved from https://www.scien.cx/2021/10/12/how-to-use-javascript-to-get-elements-by-class-name/

MLA
" » How to use JavaScript to get elements by class name?." hrishikesh1990 | Sciencx - Tuesday October 12, 2021, https://www.scien.cx/2021/10/12/how-to-use-javascript-to-get-elements-by-class-name/
HARVARD
hrishikesh1990 | Sciencx Tuesday October 12, 2021 » How to use JavaScript to get elements by class name?., viewed ,<https://www.scien.cx/2021/10/12/how-to-use-javascript-to-get-elements-by-class-name/>
VANCOUVER
hrishikesh1990 | Sciencx - » How to use JavaScript to get elements by class name?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/12/how-to-use-javascript-to-get-elements-by-class-name/
CHICAGO
" » How to use JavaScript to get elements by class name?." hrishikesh1990 | Sciencx - Accessed . https://www.scien.cx/2021/10/12/how-to-use-javascript-to-get-elements-by-class-name/
IEEE
" » How to use JavaScript to get elements by class name?." hrishikesh1990 | Sciencx [Online]. Available: https://www.scien.cx/2021/10/12/how-to-use-javascript-to-get-elements-by-class-name/. [Accessed: ]
rf:citation
» How to use JavaScript to get elements by class name? | hrishikesh1990 | Sciencx | https://www.scien.cx/2021/10/12/how-to-use-javascript-to-get-elements-by-class-name/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.