change background color by class name javascript

Change Background Color by Class Name with JavaScript

By Thomas Lopez
AD

In the dynamic world of web development, altering the appearance of HTML elements is a common task. One way to achieve this is by changing the background color of an element using JavaScript. Specifically, you can target elements by their class name to apply consistent styles across your webpage. Let's delve into how to change the background color of elements by their class name using JavaScript.

Before we jump into the JavaScript part, let's ensure we have a basic understanding of HTML. In your HTML document, you can assign a class name to elements using the 'class' attribute. For instance, an HTML paragraph might look like this:

Hello, World!

AD
🎨 CSS Colors & Backgrounds | Style Your Website Beautifully (Lesson 3)
🎨 CSS Colors & Backgrounds | Style Your Website Beautifully (Lesson 3)
Change the Background Color using JavaScript / HTML & CSS
Change the Background Color using JavaScript / HTML & CSS
JavaScript: How to randomly change background color
JavaScript: How to randomly change background color
. In this example, we've assigned the class 'yellow-background' to this paragraph.

The getElementsByClassName Method

The getElementsByClassName method allows us to select elements based on their class name. This method returns a live HTMLCollection of elements, which means it automatically updates itself when the document changes. Here's the basic syntax:

JavaScript Tips to Change Border Radius & Background Color
JavaScript Tips to Change Border Radius & Background Color
Change Background Color in JavaScript | HTML and CSS Tutorial
Change Background Color in JavaScript | HTML and CSS Tutorial
CSS Background – Everything You Need To Know — Smashing Magazine
CSS Background – Everything You Need To Know — Smashing Magazine

var elements = document.getElementsByClassName('className');

Basic Usage

Once you've selected the elements, you can change their background color using the 'style' property. Let's see a simple example:

AD

document.getElementsByClassName('yellow-background')[0].style.backgroundColor = 'blue';

Generate a CSS Color Gradient
Generate a CSS Color Gradient
a notebook with some notes on it and the words css colors and backgrounds written in blue
a notebook with some notes on it and the words css colors and backgrounds written in blue
Pure CSS Background Color Change Animation
Pure CSS Background Color Change Animation
CSS Gradient Card Border Animation
CSS Gradient Card Border Animation
10+ Best Javascript Color Picker Libraries
10+ Best Javascript Color Picker Libraries
Color Guessing Game JavaScript With Source Code
Color Guessing Game JavaScript With Source Code

In this JavaScript code snippet, we're selecting the first element with the class 'yellow-background' and changing its background color to blue.

Bulk Changes with a Loop

What if you have multiple elements with the same class name, and you want to change all of them at once? This is where loops come in handy. Here's how you can change the background color of all elements with the class 'yellow-background':

AD

var elements = document.getElementsByClassName('yellow-background');
for (var i = 0; i < elements.length; i++) {
elements[i].style.backgroundColor = 'blue';
}

Button
Button

This code loops through all elements with the class 'yellow-background' and changes their background color to blue.

The querySelectorAll Method

Another way to target elements by class name is to use the querySelectorAll method. This method returns a static NodeList, which means it doesn't automatically update when the document changes. The syntax is as follows:

AD

var elements = document.querySelectorAll('.className');

Unique Selectors

One advantage of querySelectorAll is that it supports CSS selectors, allowing for more specific targeting. For example, you can select the first paragraph with the class 'yellow-background' using 'p.yellow-background'. Here's how you can change its background color:

document.querySelector('.yellow-background').style.backgroundColor = 'blue';

In this case, querySelector returns the first matching element, eliminating the need for a loop if you're only targeting one element.

Mutating the NodeList

QuerySelectorAll returns a NodeList, which can be mutated using methods like forEach. Here's how you can change the background color of all elements with the class 'yellow-background':

document.querySelectorAll('.yellow-background').forEach(function(element) {
element.style.backgroundColor = 'blue';
});

This code does the same thing as the loop example in the previous section, but it's a bit more concise.

Using Modern JavaScript (ES6)

If you're using modern JavaScript, you can take advantage of arrow functions and let/const declarations for cleaner and more concise code. Here's an up-to-date version of the querySelectorAll example with a forEach loop:

const elements = document.querySelectorAll('.yellow-background');
elements.forEach((element) => {
element.style.backgroundColor = 'blue';
});

This code does the same thing as the previous example, but it's written using ES6 syntax.

And there you have it! You now know how to change the background color of elements by their class name using JavaScript. Whether you're using getElementsByClassName, querySelectorAll, or modern JavaScript syntax, you're equipped to bring your webpages to life with dynamic styling. Happy coding!

AD