Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
46
rated 0 times [  50] [ 4]  / answers: 1 / hits: 47049  / 13 Years ago, wed, april 27, 2011, 12:00:00

I am using a simple script to find an image on a page and get its source.



function img_find() {
var img_find2 = document.getElementsByTagName(img)[0].src;
return img_find;
}


However when I go to write this function on my page it only finds the first image and then stops. What is the best way to have it print all of the image src's on the current page?
Thanks!


More From » image

 Answers
14

You indeed told the code to do so. Don't do that. Just tell it to loop over all images and push the src of each in an array and return the array with all srcs instead.



function img_find() {
var imgs = document.getElementsByTagName(img);
var imgSrcs = [];

for (var i = 0; i < imgs.length; i++) {
imgSrcs.push(imgs[i].src);
}

return imgSrcs;
}

[#92524] Tuesday, April 26, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
martin

Total Points: 405
Total Questions: 93
Total Answers: 93

Location: Mexico
Member since Sun, Jul 25, 2021
3 Years ago
;