Element.getElementsByTagName() to get all <img> elements inside the provided element.Array.prototype.map() to map every src attribute of each <img> element.includeDuplicates is false, create a new Set to eliminate duplicates and return it after spreading into an array.includeDuplicates, to discard duplicates by default.const getImages = (el, includeDuplicates = false) => { const images = [...el.getElementsByTagName('img')].map(img => img.getAttribute('src') ); return includeDuplicates ? images : [...new Set(images)]; };
getImages(document, true); // ['image1.jpg', 'image2.png', 'image1.png', '...'] getImages(document, false); // ['image1.jpg', 'image2.png', '...']
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️