JQuery Get and Set Image Src Example

jquery get set image src


How to get the image src and set the image src using jQuery attr(). In this article, you will learn how to get and set the image src using jQuery.

The attr() method to get and change the image source in jQuery.

Get Image Src

Use the below script to get image src.

$(document).ready(function() {
        $imgsrc = $('img').attr('src');

Set the Image Src

Use the below script to set the image src.

$(document).ready(function() {

Let’s take an example

The following example will change the second image to the first image

      <title> How to get and set image src attribute example</title>
      <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>   
         $(document).ready(function() {
                $imgsrc = $('#first').attr('src');
            margin: 30px;
    <div class="card">
        <img src="//image-name-with-source.jpeg" width="100" id="first" alt="Poker Card">
    <div class="card">
        <img src="//image-name-with-source.jpeg" width="100" id="second" alt="Poker Card">
   <button type="type" class="btn">Get &amp; Set image src</button>

Check This Also

Difference between AngularJS and Angular 2 Make a Mobile app in just 10 minutes With Ionic Framework Angular 9: What to Expect in New Version of Angular Alert (Toaster) Notifications in Angular 8

Tags: website development company in delhi|| website designing company in delhi || b2b portal development company || magento development company in delhi ||  website redesigning company in delhi

About Post Author

Leave a Reply

Your email address will not be published.