JQuery Get and Set Image Src Example Tutorial

Jul 24, 2021 . Admin

Hi Dev,

Today, In this tutorial I will explain you how to get and set image src in jquery. i will explain show easiest example of jquery get and set image src.The attr()

method to get and change the image source in jQuery.

Here, i will give you a full example of how to get and set image src in jquery.

Get Image Src

In this example,I will Use the below script to get image src.

$(document).ready(function() {
  $('.btn').click(function(){
    $imgsrc = $('img').attr('src');
    alert($imgsrc);
   });
});
Set the Image Src

In this example,I will Use the below script to set image src.

$(document).ready(function() {
  $('.btn').click(function(){
    $("img").attr("src",'test.png');
  });
});
Full Example

Now,The following example will change the second image to the first image.



   How To Get And Set Image src Attribute Example - MyWebTuts.com
     
  
  


    

How To Get And Set Image src Attribute Example - MyWebTuts.com

Blog Image
Blog Image

It Will help you....

#Jquery