• 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 / Using JavaScript to Change the Image src

Using JavaScript to Change the Image src

January 30, 2022 2 Comments

To change the src of an image using JavaScript, we simply 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 change the image src of #img1 from img.jpg to anotherImg.jpg, we will use the src property in the following JavaScript code:

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

Using JavaScript to Change the Image src with a Click

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

Let’s say we have the same HTML from above.

<div id="div1">
  <img id="img-1" src="http://theprogrammingexpert.com/wp-content/uploads/example-img1.png">
  <div id="click-me" onclick="changeIMG()">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 change the image from the example-img1.png version to the example-img2.png version.

Here is the function below that will accomplish this:

function changeIMG(){
  document.getElementById("img-1").src = "http://theprogrammingexpert.com/wp-content/uploads/example-img2.png";
};

The final code and output for this example of how to change the source of an image using the src property and 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="changeIMG()">Change image</div>
</div>

<script>

function changeIMG(){
  document.getElementById("img-1").src = "http://theprogrammingexpert.com/wp-content/uploads/example-img2.png";
};

</script>

Using JavaScript to Toggle Image src with a Click

To toggle the source of an image we simply need access to another image, or use our original image URL.

We will use the same HTML.

<div id="div1">
  <img id="img-2" src="http://theprogrammingexpert.com/wp-content/uploads/example-img1.png">
  <div id="click-me" onclick="toggleIMG()">Toggle 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 will toggle between these two images.

We will modify our function slightly to toggle the image instead of just changing it once.

Here is the function below that will accomplish this:

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

The final code and output for this example of how to toggle the source of an image using the src property and JavaScript is below:

Code Output:

Toggle image

Full Code:

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

<script>

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

</script>

Hopefully this article has helped you understand how to use JavaScript to change the image src of an image.

Other Articles You'll Also Like:

  • 1.  Add Hours to a Date Using JavaScript
  • 2.  Using JavaScript to Compare Dates
  • 3.  How to Create a New h1 Element With JavaScript
  • 4.  How to Iterate through an Array Using JavaScript
  • 5.  How to Convert a String to Lowercase In JavaScript
  • 6.  How to Hide a Div in JavaScript
  • 7.  How to Select All Checkboxes in JavaScript
  • 8.  Creating a JavaScript Function to Add Two Numbers
  • 9.  Remove the First and Last Character From a String in JavaScript
  • 10.  Using JavaScript to Remove Backslash From a String

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

Comments

  1. Berdly says

    August 22, 2022 at 5:26 am

    thanks it helped me
    but i need to ask you ?
    how to toggle img src onclick?

    Reply
    • Tom says

      September 13, 2022 at 10:04 am

      Hey Berdly. We added a new section to this post at the end to show how to toggle an img src onclick. Hope that helps!

      Reply

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