• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Using the getElementsByClassName Method in JavaScript

Using the getElementsByClassName Method in JavaScript

February 10, 2022 Leave a Comment

To get multiple HTML elements using JavaScript, we can use the getElementsByClassName() method if all the elements share the same classname.

document.getElementsByClassName("classes")

The getElementsByClassName() method will return a collection of HTML elements. In this case, it will be HTML elements with the class name of “classes”.


Let’s say I have the following HTML:

<div id="div1">
  <p class="p">This is a paragraph.</p>
  <p class="p">This is a paragraph.</p>
  <p class="p">This is a paragraph.</p>
  <p>This is a paragraph.</p>
  <p>This is a paragraph.</p>
  <p>This is a paragraph.</p>
</div>

If we want to make all the paragraphs with class “p” bold, we can do this with the getElementsByClassName() method and a for loop. The getElementsByClassName method will get all the elements with the class name we want. We then have to loop through each one to set the individual style for that element.

var allClasses = document.getElementsByClassName("p");
for (var i = 0; i < allClasses.length; i++) {
  allClasses[i].style.fontWeight = "bold";
}

The resulting HTML would be as follows:

This is a paragraph.

This is a paragraph.

This is a paragraph.

This is a paragraph.

This is a paragraph.

This is a paragraph.

Using the getElementsByClassName Method With a Click

We can change multiple elements with the same class in HTML using JavaScript very easily by utilizing the getElementsByClassName method with an onclick event.

Let’s say we have the following HTML code and we want to give the user the ability to change the styles of certain classes. The elements below will alternate with different classes, “odd”, and “even”. We will change the background color of the odd classes to give the divs a table-like style.

Here is the HTML:

<div class="odd">
  <p class="p">This is an odd paragraph.</p>
</div>
<div class="even">
  <p class="p">This is an even paragraph.</p>
</div>
<div class="odd">
  <p class="p">This is an odd paragraph.</p>
</div>
<div class="even">
  <p class="p">This is an even paragraph.</p>
</div>
<div class="odd">
  <p class="p">This is an odd paragraph.</p>
</div>
<div class="click-me" onclick="clickFunction()">Add styles</div>

We can utilize both the onclick event

and the getElementsByClassName() to change the background of each div with classname “odd”.

Below is the Javascript code which will allow the user to be able to do this:

function clickFunction() {
  var allClasses = document.getElementsByClassName("odd");
  for (var i = 0; i < allClasses.length; i++) {
    allClasses[i].style.backgroundColor = "#ffffff";
  }
}

The final code and output for this example of using getElementsByClassName with JavaScript is below:

Code Output:

This is an odd paragraph.

This is an even paragraph.

This is an odd paragraph.

This is an even paragraph.

This is an odd paragraph.

Add styles

Full Code:

<div class="odd">
  <p class="p">This is an odd paragraph.</p>
</div>
<div class="even">
  <p class="p">This is an even paragraph.</p>
</div>
<div class="odd">
  <p class="p">This is an odd paragraph.</p>
</div>
<div class="even">
  <p class="p">This is an even paragraph.</p>
</div>
<div class="odd">
  <p class="p">This is an odd paragraph.</p>
</div>
<div class="click-me" onclick="clickFunction()">Add styles</div>

<script>

function clickFunction() {
  var allClasses = document.getElementsByClassName("odd");
  for (var i = 0; i < allClasses.length; i++) {
    allClasses[i].style.backgroundColor = "#ffffff";
  }
}

</script>

Hopefully this article has been useful for you to understand how to use the getElementsByClassName method with JavaScript.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Check if String Contains Letters
  • 2.  Remove Commas From Array in JavaScript
  • 3.  Using JavaScript to Return Two Values
  • 4.  How in JavaScript to Get the Day of the Week
  • 5.  JavaScript Coin Flip – How to Simulate Flipping a Coin in JavaScript
  • 6.  Using JavaScript to Change the href of a Link
  • 7.  JavaScript offsetTop – Get the Top Position of Element
  • 8.  Using JavaScript to Get the Length of Array
  • 9.  How to Swap Values in an Array in JavaScript
  • 10.  Using JavaScript to Wait 5 Seconds Before Executing Code

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy