• 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 / jQuery / Using jQuery to Change Background Color

Using jQuery to Change Background Color

November 25, 2021 Leave a Comment

Changing the background color of a div using jQuery is done simply by using the css() method.

$("#div").css("background-color", "green");

Let’s say we have the following HTML:

<div id="div">
  <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 this div to green, we will use the jQuery css() method in the following JavaScript code.

$("#div").css("background-color", "green");

If you are using WordPress, don’t forget to change the $ to jQuery as below:

jQuery("#div").css("background-color", "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.

$("#div").css("background-color", "#00FF00");

Using jQuery to Change Background Color of a Div with a Click

Many times when creating a web page and the user experience, we want to change the color of a div and interact with another element on the web page.

To change the background color of a div using jQuery, we can combine the css() method 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 id="click-me">Change background color of #div1</div>
</div>

We can utilize both the jQuery click() method and jQuery css() method to change background color.

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

$("#click-me").click(function(){
  $("#div1").css("background-color", "#7bbfa2");
}); 

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

Code Output:

Div 1
Div 2
Change background color of #div1

Full Code:

<div>
  <div id="div1">Div 1</div>
  <div id="div2">Div 2</div>
  <div id="click-me">Change background color of #div1</div>
</div>

<script>

$("#click-me").click(function(){
  $("#div1").css("background-color", "#7bbfa2");
});

</script>

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

Other Articles You'll Also Like:

  • 1.  Using jQuery to Replace a Class
  • 2.  Using jQuery to Disable a Button
  • 3.  jQuery focusin – Changing the Background Color with the focusin() Method
  • 4.  Using jQuery to Change Image src
  • 5.  Changing the Background Image of a div Using jQuery
  • 6.  jQuery Opacity – How to Change the Opacity of an Element
  • 7.  jQuery appendTo – Insert Element As Last Child
  • 8.  Using jQuery to Remove Class from an HTML Element
  • 9.  Using jQuery to Toggle Class of HTML Element
  • 10.  jQuery slideUp – How to Slide Up and Hide an Element Using jQuery

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

x