• 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 / JavaScript Change Background Color of Element

JavaScript Change Background Color of Element

December 2, 2021 Leave a Comment

Changing the background color of a div using JavaScript is done simply by using the backgroundColor property.

document.getElementById("div1").style.backgroundColor = "green";

Let’s say we have the following HTML:

<div id="div1">
  <p>This paragraph is in a div that we need to change the color of.</p>
</div>

If we want to change the background color of #div1 to green, we will use the backgroundColor property in the following JavaScript code.

document.getElementById("div1").style.backgroundColor = "green";

The second argument can be any valid css color. So for example, we could use the hexadecimal value for green to change the background color to green.

document.getElementById("div1").style.backgroundColor = "#00FF00";

Using JavaScript to Change the Background Color of a Div with a Click

To change the background color of a div using JavaScript, we can target the backgroundColor property with a click event.

Let’s say we have the following HTML and we want to change the background color of #div1 to green.

<div>
  <div id="div1">Div 1</div>
  <div id="div2">Div 2</div>
  <div class="click-me" onclick="clickFunction()">Change background</div>
</div>

We can utilize both the backgroundColor property of #div1 along with an onclick event to change the background color.

Below is the JavaScript code which will allow the user to be able to update the background color of the div:

function clickFunction() {
  var currDiv = document.getElementById("div1");
  currDiv.style.backgroundColor = "green";
}

The final code and output for this example of how to change the background color of our div using JavaScript is below:

Code Output:

Div 1
Div 2

Change background

Full Code:

<div>
  <div id="div1">Div 1</div>
  <div id="div2">Div 2</div>
  <div class="click-me" onclick="clickFunction()">Change background</div>
</div>

<script>

function clickFunction() {
  var currDiv = document.getElementById("div1");
  currDiv.style.backgroundColor = "green";
}

</script>

Hopefully this article has been useful for you to understand how to change the background color of an element using JavaScript.

Other Articles You'll Also Like:

  • 1.  Using Javascript to Remove Class from Element
  • 2.  Using JavaScript to Replace Character in String
  • 3.  JavaScript slice – How to Get a Substring From a String
  • 4.  Using JavaScript to Remove Forward Slash From String
  • 5.  Creating a JavaScript Function to Multiply Two Numbers
  • 6.  JavaScript Infinite Loop
  • 7.  JavaScript acos – Find Arccosine and Inverse Cosine of Number
  • 8.  Using JavaScript to Resize an Image
  • 9.  Using JavaScript to Check if String Contains Only Letters
  • 10.  setInterval JavaScript – How to Repeat Code in Set Time Intervals

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