Simply use the querySelectorAll
method on document
. You can then map the returned NodeList to an array of src
attributes.
var images = document.querySelectorAll('.e1 img[src]'),
sources = Array.prototype.map.call(images, function(img) {
return img.src;
});
I've used the above selector '.e1 img[src]'
to make sure that the returned NodeList
only has images with src
attributes.
JSFiddle Demo ~ http://jsfiddle.net/u9Lkada3/1/
Don't forget! You need to run the script after the elements exist in the document. The easiest way to ensure this is to put your script at the end, just before the closing </body>
tag.