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

Using jQuery to Change Image src

November 26, 2021 Leave a Comment

To change the image src using jQuery, the simplest way is to use the jQuery attr() method:

$("#img-1").attr("src","anotherImg.jpg");

Let’s say I have the following HTML code:

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

To change the image src of #img-1 from img.jpg to anotherImg.jpg, we will use the jQuery attr() method like in the following Javascript code:

$("#img-1").attr("src","anotherImg.jpg");

If you are using WordPress, don’t forget to change the $ to jQuery as below:

jQuery("#img-1").attr("src","anotherImg.jpg");

Changing Image Src Using jQuery with a Click

Many times when creating a web page and the user experience, we want to change an image after an interaction with another element on the web page.

To change the image src using jQuery, we can combine the attr() method with a click event.

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

<div id="div1">
  <img id="img-1" src="/wp-content/uploads/2021/11/tpe-main.jpg">
  <div id="click-me">Change image</div>
</div>

We have two images, tpe-main.png is a picture of gears in png form, and tpe-main.jpg is the same image, but in jpg form.

If we want to change the image from the .jpg version to the .png version when we click the #click-me div, we can add the following Javascript code to accomplish this:

$("#click-me").click(function(){
  $("#img-1").attr("src","/wp-content/uploads/2021/11/tpe-main.png");
}); 

The final code and output for this example of how to change the source of an image using the jQuery attr() method and Javascript is below:

Code Output:

Change image

Full Code:

<div id="div1">
  <img id="img-1" src="/wp-content/uploads/2021/11/tpe-main.jpg">
  <div id="click-me">Change image</div>
</div>

<script>

$("#click-me").click(function(){
  $("#img-1").attr("src","/wp-content/uploads/2021/11/tpe-main.png");
}); 

</script>

Hopefully this article has helped you understand how you can update the image source (src) using jQuery.

Other Articles You'll Also Like:

  • 1.  Using jQuery to Get the Position of Element
  • 2.  Using jQuery to Remove All Options From Select
  • 3.  jQuery focusin – Changing the Background Color with the focusin() Method
  • 4.  Using jQuery to Add a Sibling to an Element
  • 5.  Using jQuery to Change Background Color
  • 6.  jQuery first() – Get the First Element
  • 7.  jQuery slideUp – How to Slide Up and Hide an Element Using jQuery
  • 8.  How to Use jQuery to Check if an Element is Visible
  • 9.  Using jQuery to Change Inner HTML
  • 10.  Using jQuery to Check if Checkbox is Checked

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