• 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 / 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.  How to Clear an Input Field Using jQuery
  • 2.  Using jQuery to Swap an Image on Hover
  • 3.  jQuery after – Insert HTML After Another Element
  • 4.  Using jQuery to Scroll to Div
  • 5.  jQuery hasClass – How to Check if an Element Has a Certain Class
  • 6.  Using jQuery to Hide a Div
  • 7.  Using jQuery to Select Multiple Classes
  • 8.  jQuery width() – Using jQuery to Get the Width of a Div
  • 9.  How to Set Checkbox Checked With jQuery
  • 10.  Using jQuery to Append Text to textarea Field

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