• 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 / Changing the Background Image of a div Using jQuery

Changing the Background Image of a div Using jQuery

March 30, 2022 Leave a Comment

Changing the background image using jQuery is done simply by using the jQuery css() method and the background-image property.

$("#div1").css("background-image", "url('someImage.jpg')");

Let’s say we have the following HTML with the following style:

<style>#div1 { background-image: url('img.jpg'); }</style>
<div id="div1"></div>

To change the background image of #div1 from img.jpg to anotherImg.jpg, we will use the background-image property along with the css() method in the following jQuery code:

$("#div1").css("background-image", "url('anotherImg.jpg')");

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

jQuery("#div1").css("background-image", "url('anotherImg.jpg')");

We can also do this in pure Javascript using the backgroundImage property.

Changing the Background Image of a Div in jQuery with a Click

To change the background image 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 image of #div1 from example-img1.png to example-img2.png. Both images are the same image of gears, but with different colors.


<style>#div1 { width: 150px; height: 150px; background-image: url('http://theprogrammingexpert.com/wp-content/uploads/example-img1.png'); }</style>
<div id="div1"></div>
<div id="click-me">Change background image</div>

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

We will add some code so that the user can switch between both images as many times as they want.

Here is the jQuery code for changing a background image:

$("#click-me").click(function(){
  //Here we are just checking which image is showing, and displaying the other
  if( $("#div1").css("background-image").indexOf('1') > -1 ) {
    $("#div1").css("background-image", "url('http://theprogrammingexpert.com/wp-content/uploads/example-img2.png')");
  } else {
    $("#div1").css("background-image", "url('http://theprogrammingexpert.com/wp-content/uploads/example-img1.png')");
  }
});

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

Code Output:

Change background image

Full Code:


<style>#div1 { width: 150px; height: 150px; background-image: url('http://theprogrammingexpert.com/wp-content/uploads/example-img1.png'); }</style>
<div id="div1"></div>
<div id="click-me">Change background image</div>

<script>

$("#click-me").click(function(){
  if( $("#div1").css("background-image").indexOf('1') > -1 ) {
    $("#div1").css("background-image", "url('http://theprogrammingexpert.com/wp-content/uploads/example-img2.png')");
  } else {
    $("#div1").css("background-image", "url('http://theprogrammingexpert.com/wp-content/uploads/example-img1.png')");
  }
});

</script>

Hopefully this article has been useful for you to understand changing the background image in jQuery.

Other Articles You'll Also Like:

  • 1.  Get nth Child with jQuery
  • 2.  Using jQuery to Change Image src
  • 3.  Using jQuery to Add a Sibling to an Element
  • 4.  Using jQuery to Check if Element Has Class
  • 5.  Using jQuery to Get Value of Select On Change
  • 6.  Resizing an Image Using jQuery
  • 7.  jQuery slideDown – How to Slide Down and Show an Element Using jQuery
  • 8.  jQuery Get Last Child
  • 9.  jQuery focusout – How to Use the focusout() Method on an HTML Form
  • 10.  Using jQuery to Convert a String to Lowercase

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