How to get all image sources on a specific page using Javascript

I use a simple script to find the image on the page and get its source.

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

However, when I move on to writing this function on my page, it finds only the first image and then stops. What is the best way to print the entire src image on the current page? Thank!

+5
source share
4 answers

You really said the code for this. Do not do that. Just tell it to iterate over all the images and press src of each in the array and instead return an array with all srcs.

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;
}
+19
source

, , , - .

for(var i = 0; i< document.images.length; i++){
document.images[i].style.border = "1px solid #E0FDA6";
}

, , ( ), E0FDA6 ( reset ), , .

Rg, Anjanka

0

It can help you ...

img=document.getElementsByTagName("img");
for(i=0; i<img.length; i++) {
    imgp = imgp + img[i].src + '<br/>'; 
}
document.write(imgp);
0
source

Make it simple:

console.log(document.body.getElementsByTagName('img'));
0
source

All Articles