• 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
  • VBA
  • 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.  How to Call a JavaScript Function From HTML
  • 2.  JavaScript sinh – Find Hyperbolic Sine of Number Using Math.sinh()
  • 3.  parseFloat JavaScript – How to use the JavaScript parseFloat() method
  • 4.  Using JavaScript to Remove All Pipe Characters From String
  • 5.  Using JavaScript to Get the Current Year
  • 6.  Changing the Background Image of a div in JavaScript
  • 7.  Using JavaScript Math Module to Get Euler’s Constant e
  • 8.  Using JavaScript to Increment a Variable by 1
  • 9.  Using JavaScript to Get Image Dimensions
  • 10.  Using JavaScript to Capitalize the First Letter of a String

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy