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

document.querySelectorAll('img[srcset="value"]');

Problem :

for an example ,

<img class="page-hero__image" style="" srcset="https://www.thecloroxcompany.com/wp-content/uploads/160602_fastenclorox_103813-1400x0-c-default.jpg 1600w,
                            https://www.thecloroxcompany.com/wp-content/uploads/160602_fastenclorox_103813-1200x0-c-default.jpg 1200w,
                            https://www.thecloroxcompany.com/wp-content/uploads/160602_fastenclorox_103813-1024x0-c-default.jpg 1024w,
                            https://www.thecloroxcompany.com/wp-content/uploads/160602_fastenclorox_103813-800x0-c-default.jpg 800w,
                            https://www.thecloroxcompany.com/wp-content/uploads/160602_fastenclorox_103813-400x0-c-default.jpg 400w" sizes="(min-width: 1200px) 1200px, 100vw" src="https://www.thecloroxcompany.com/wp-content/uploads/160602_fastenclorox_103813-1000x0-c-default.jpg" 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?

Comments

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 thecloroxcompany.com/wp-content/uploads/…

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 thecloroxcompany.com/wp-content/uploads/…

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=””]…

By