Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
112
rated 0 times [  116] [ 4]  / answers: 1 / hits: 58092  / 13 Years ago, fri, february 17, 2012, 12:00:00

I wanted to have a javascript function to basically return an array of all the img src there is in a page, how do I do this?


More From » javascript

 Answers
7

You can easily get an array containing all img elements via document.getElementsByTagName():



var images = document.getElementsByTagName('img'); 
var srcList = [];
for(var i = 0; i < images.length; i++) {
srcList.push(images[i].src);
}


Instead of document.getElementsByTagName('img') you could also use the document.images collection.






If you are using jQuery, you can also use $('img') which gives you a jQuery object containing all img elements.



var srcList = $('img').map(function() {
return this.src;
}).get();

[#87412] Wednesday, February 15, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
harrisa

Total Points: 514
Total Questions: 93
Total Answers: 93

Location: Ghana
Member since Sun, Mar 27, 2022
2 Years ago
;