Solution 1 :

you need to find the image element first and filter by srcset

var images = document.getElementsByTagName('img'); 
for(var i = 0; i < images.length; i++) {
    if(images[i].src == srcset) //check with your srcset
       console.log("found image element"); //image[i] has found element

or you can use this way too


Problem :

for an example ,

<img class="page-hero__image" style="" srcset=" 1600w,
                   400w" sizes="(min-width: 1200px) 1200px, 100vw" src="" alt="">

I need to select the above img element by the srcset attribute and its value using the document.querySelectorAll('img[srcset='value']').
How can I do that?


Comment posted by Jay Vaghasiya

Please provide more details and if possible add what you have done so far

Comment posted by Aswin T A

i have the above image element as a string format. so i generated a css selector using the tag name, attributes and its values. i need to select the above image element with document.querySelector( ) using the [srcset=”value”] . how can i do that

Comment posted by Alaa Aqeel

this not work with u

Comment posted by Aswin T A

I put the entire srcset attribute value and it doesn’t working

Comment posted by…

is it possible to find the element using document.querySelectorAll(‘img[srcset*=”

Comment posted by Aswin T A

its only working with single value like this document.querySelectorAll(

Comment posted by AakashRajni

you should put all srcset inside the queryselector and replace the doublequotes in srcset to singlequotes and then try

Comment posted by…

i tried it and not working. i tried with two values in the srcset attribute like this : document.querySelectorAll(‘img[[srcset*=”

Comment posted by AakashRajni

try using multiple srcset img[srcset=””][srcset=””][srcset=””]…