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"]');
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?
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
is it possible to find the element using document.querySelectorAll(‘img[srcset*=”
you should put all srcset inside the queryselector and replace the doublequotes in srcset to singlequotes and then try
i tried it and not working. i tried with two values in the srcset attribute like this : document.querySelectorAll(‘img[[srcset*=”