JavaScript - Dynamically Change Background Image

In this tutorial we will create a Dynamically Change Background Image using JavaScript. This code will immediately change your background image when the user click a image. The code use onclick() function to initiate a method that change your image background source base on the click image. You can apply this script to your code to make your transaction faster, it is a user-friendly program feel free to modify it. We will use JavaScript to add some new feature to the website interface by actually written into an HTML page. It is what gives your page a different interactive elements and animation that engage a user.

Getting Started:

This is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7.         <nav class="navbar navbar-default">
  8.                 <div class="container-fluid">
  9.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10.                 </div>
  11.         </nav>
  12.         <div class="col-md-3"></div>
  13.         <div class="col-md-6 well">
  14.                 <h3 class="text-primary">JavaScript - Dynamically Change Background Image</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <a href="index.html" class="btn btn-success"><span class="glyphicon glyphicon-refresh"></span></a>
  17.                 <br /><br />
  18.                 <div class="col-md-3"></div>
  19.                 <div class="col-md-6" style="border:1px solid #000; height:200px; padding-top:10%; padding-left:10%;" id="display">
  20.                         <h2>Image HERE</h2>
  21.                 </div>
  22.                 <br style="clear:both;"/><br />
  23.                 <div class="col-md-12" style="border:1px solid #000; padding:10px;">
  24.                         <img src="images/image1.jpg" onclick="changeBackground(this)" height="150" width="150"/>
  25.                         <img src="images/image2.jpg" onclick="changeBackground(this)" height="150" width="150"/>
  26.                         <img src="images/image4.jpg" onclick="changeBackground(this)" height="150" width="150"/>
  27.                         <img src="images/image3.jpg" onclick="changeBackground(this)" height="150" width="150"/>
  28.                 </div>
  29.                 <br style="clear:both;"/><br />
  30.         </div>
  31. <script src="js/script.js"></script>
  32. </body>
  33. </html>

Creating the Script

This code contains the script of the application. This code will change image background when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory
  1. function changeBackground(image){
  2.         document.getElementById('display').innerHTML="";
  3.         document.getElementById('display').style.backgroundImage="url('"+image.src+"')";
  4.         document.getElementById('display').style.backgroundSize="cover";
  5. }
There you have it we successfully created a Dynamically Change Background Image using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment