Mahi
Mahi

Reputation: 593

How to pass image src to srcset for the same image using jquery

Hi I have the below image tag

<img class="CaptureViewer-image" src="data:image/jpeg;base64 alt="Photo of your document" aria-hidden="true">

I have the below image src using JQuery

$(".CaptureViewer-image").attr("src");

I want to pass the same image src to add the srcset in the above tag. I tried like this

var imagesrc = $(".CaptureViewer-image").attr("src);

$(".CaptureViewer-image").attr("srcset" , "imagesrc 300px");

when checked for the srcset value , the value is getting saved as imagesrc 300px instead of actual image source . I am trying to find the solution and learn in the process.Thanks.

Upvotes: 0

Views: 124

Answers (1)

kobi2c
kobi2c

Reputation: 447

use each :

$(".CaptureViewer-image").each ( function() {
  var elem = $(this);
  elem.attr("srcset" , elem.attr('src') + " 300px");
});

Upvotes: 2

Related Questions