Get Elements By Multiple Class Names In JavaScript

Get Elements by multiple Class Names in JavaScript

You can use getElementsByClassName() or querySelectorAll() method to get elements by multiple class names in JavaScript. Specific steps will be introduced in this article. Let’s check it out.

How to get elements by multiple class names in JavaScript

In this article, we give three methods with specific cases to help you know how to get elements by multiple class names in JavaScript:

  • Using the getElementsByClassName() method
  • Using the querySelectorAll() method
  • Using the querySelector() method

We will use the following HTML code throughout this article.

Code:

<!DOCTYPE html>
<html>
    <head>
        <style>
            body {
            	padding: 20px 50px;
            }
            .text {
              	font-size: 20px;
            }
            .red {
              	color: red;
            }
            .blue {
              	color: blue;
            }
            .thick {
              	font-weight: bold;
            }
        </style>
    </head>
    <body>
        <p class="text red">LearnShareIT</p>
        <p class="text red thick">LearnShareIT</p>
        <p class="text blue">LearnShareIT</p>
        <p class="text blue thick">LearnShareIT</p>

        <script src="script.js"></script>
    </body>
</html>

Output:

Now we will get elements by multiple class names using JavaScript on this HTML code with different methods.

Using the getElementsByClassName() method

The getElementsByClassName() method returns an HTMLCollection that looks like an array, an HTMLCollection containing elements with all the specified class names.

Syntax:

document.getElementsByClassName(classname)

Parameter:

  • classname: The class name of the specified elements.

Code:

// Get all the elements of 'text' class and 'thick' class
const collection = document.getElementsByClassName('text thick');

collection[0].style.backgroundColor = "yellow";
console.log(collection);

Console:

[p.text.red.thick, p.text.blue.thick]

In the above code, we use getElementsByClassName() to get all the elements of ‘text’ class and ‘thick’ class and store them in a HTMLCollection named “collection”.

We then take the element with index 0 and set the backgroundColor property to ‘yellow’.

Output:

Using the querySelectorAll() method

The querySelectorAll() method returns a NodeList containing all elements that match one or more CSS selectors.

Syntax:

document.querySelectorAll(CSS selectors)

Parameters:

  • CSS selectors: One or more CSS selectors (separated by “,”).

Code:

// Get all elements of CSS selectors '.text' or '.thick'
const nodelist = document.querySelectorAll('.text, .thick');

nodelist[0].style.backgroundColor = "yellow";
console.log(nodelist);

Console:

[p.text.red, p.text.red.thick, p.text.blue, p.text.blue.thick]

In the above code, we use querySelectorAll() to get all elements of CSS selectors ‘.text’ or ‘.thick’ and store them in a NodeList named “nodelist”.

Then we take the element with index 0 and set the backgroundColor property to ‘yellow’.

Output:

Using the querySelector() method

The querySelector() method returns the first element of a NodeList containing all elements that match one or more CSS selectors.

Syntax:

document.querySelector(CSS selectors)

Parameters:

  • CSS selectors: One or more CSS selectors (separated by “,”).

Code:

// Get the first element of CSS selectors '.red' or '.thick'
const node = document.querySelector('.red, .thick');

node.style.backgroundColor = "yellow";
console.log(node);

Console:

p.text.red.thick

In the above code, we use the querySelector() method to get the first element of CSS selectors ‘.red’ or ‘.thick’. Then we set the backgroundColor property to ‘yellow’ for the newly received element.

Output:

Summary

This article has shown how to get elements by multiple class names in JavaScript. I hope the information in this article will be helpful to you. If you have any problems, please comment below. Thank you for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *