• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
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.  Using jQuery to Delete an Element
  • 2.  Using jQuery to Hide a Div
  • 3.  Using jQuery to Remove All Options From Select
  • 4.  Using jQuery to Change Label Text
  • 5.  Using jQuery to Set the Id of a Div
  • 6.  How to Use jQuery to Change Button Text
  • 7.  How to Set All Checkbox to Checked in jQuery
  • 8.  Using jQuery to Add CSS Important Style
  • 9.  Using jQuery to Empty the Contents of an HTML Element
  • 10.  How to Clear an Input Field 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 *

Copyright © 2023 · The Programming Expert · About · Privacy Policy