• 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
  • VBA
  • About
You are here: Home / JavaScript / Swapping Images in JavaScript

Swapping Images in JavaScript

February 26, 2022 Leave a Comment

Swapping Images in JavaScript is simple, we just have to use the image src property along with the getElementById method.

document.getElementById("img1").src = "anotherImg.jpg";

Let’s say I have the following HTML code:

<div id="div1">
  <img id="img1" src="img.jpg">
</div>

To swap the image above to another image, we can use the src property. We can change the src of #img1 from img.jpg to anotherImg.jpg in the following JavaScript code:

document.getElementById("img1").src = "anotherImg.jpg";

Swapping Images in JavaScript with a Click

To change the source of an image we can combine the src property with an onclick event.

Let’s say we have the following HTML, similar to our example from above.

<div id="div1">
  <img id="img-1" src="http://theprogrammingexpert.com/wp-content/uploads/example-img1.png">
  <div id="click-me" onclick="swapImages()">Change image</div>
</div>

We have two images, example-img1.png is a picture of gears and example-img2.png is the same image, but with different colors.

We have added an onclick event to the #click-me div which will run a function where we can swap the images from the example-img1.png version to the example-img2.png version.

We can add a little more code so that whenever the button is pressed, it will swap the images.

Here is the function below that will accomplish this:

function swapImages(){
  //The if statement below is simply to check which image is currently showing
  //If 1 is found in the image source file name, it means example-img1.png is showing
  //So we need to show the other image
  if( document.getElementById("img-1").src.indexOf(1) > -1 ){
    document.getElementById("img-1").src = "http://theprogrammingexpert.com/wp-content/uploads/example-img2.png";
  } else {
    //Otherwise example-img1.png is currently showing, so we must swap it
    document.getElementById("img-1").src = "http://theprogrammingexpert.com/wp-content/uploads/example-img1.png";
  }
};

The final code and output for swapping images in JavaScript is below:

Code Output:

Change image

Full Code:

<style>#img-1 { height:100px; }</style>
<div id="div1">
  <img id="img-1" src="http://theprogrammingexpert.com/wp-content/uploads/example-img1.png">
  <div id="click-me" onclick="swapImages()">Change image</div>
</div>

<script>

function swapImages(){
  if( document.getElementById("img-1").src.indexOf(1) > -1 ){
    document.getElementById("img-1").src = "http://theprogrammingexpert.com/wp-content/uploads/example-img2.png";
  } else {
    document.getElementById("img-1").src = "http://theprogrammingexpert.com/wp-content/uploads/example-img1.png";
  }
};

</script>

Hopefully this article has helped you understand swapping images in JavaScript.

Other Articles You'll Also Like:

  • 1.  Check if Checkbox is Checked in JavaScript
  • 2.  JavaScript Ceiling – Using Math.ceil() Method to Round Up to Ceiling
  • 3.  Using JavaScript to Get the Decimal Part of a Number
  • 4.  JavaScript Coin Flip – How to Simulate Flipping a Coin in JavaScript
  • 5.  Using JavaScript to Add Leading Zeros
  • 6.  How to Create a JavaScript Count Up Timer
  • 7.  JavaScript Exponent – Exponentiate Numbers with Math.pow()
  • 8.  Remove Undefined Values From an Array in JavaScript
  • 9.  parseFloat JavaScript – How to use the JavaScript parseFloat() method
  • 10.  Using JavaScript to Remove Null Values From an Array

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy