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

Changing the Background Image of a div in JavaScript

March 30, 2022 Leave a Comment

Changing the background image of a div using JavaScript is done simply by using the getElementById method along with the backgroundImage property.

document.getElementById("div1").style.backgroundImage = "url('someImage.jpg')";

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

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

To change the background image of #div1 from img.jpg to anotherImg.jpg, we will use the backgroundImage property in the following JavaScript code:

document.getElementById("div1").style.backgroundImage = "url('anotherImg.jpg')";

We can also do this in jQuery using the css() method.

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

To change the background image of a div using JavaScript, we can target the backgroundImage property 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.


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

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

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

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

function changeImage(){
  //Here we are just checking which image is showing, and displaying the other
  if( document.getElementById("div1").style.backgroundImage.indexOf('1') > -1 ) {
    document.getElementById("div1").style.backgroundImage = "url('http://theprogrammingexpert.com/wp-content/uploads/example-img2.png')";
  } else {
    document.getElementById("div1").style.backgroundImage = "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 JavaScript is below:

Code Output:

Change background image

Full Code:


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

<script>

function changeImage(){
  if( document.getElementById("div1").style.backgroundImage.indexOf('1') > -1 ) {
    document.getElementById("div1").style.backgroundImage = "url('http://theprogrammingexpert.com/wp-content/uploads/example-img2.png')";
  } else {
    document.getElementById("div1").style.backgroundImage = "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 JavaScript.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Add Seconds to a Date
  • 2.  Uncheck a Radio Button Using JavaScript
  • 3.  JavaScript Capitalize First Letter of Every Word
  • 4.  Using JavaScript to Remove Quotes From a String
  • 5.  Using JavaScript to Get the Current URL
  • 6.  Using JavaScript to Check If String is a Number
  • 7.  Using JavaScript to Check if String Contains Numbers
  • 8.  JavaScript onfocus – Changing Form Styles with the onfocus Event
  • 9.  How to Get the First Character of a String in JavaScript
  • 10.  Using JavaScript to Reverse a Number

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