Is it possible for me to get the href of all images on a page using JavaScript? This code gives me the src for those images, but I want to return the href for them.
function checkimages() {
var images = document.images;
for (var i=0; i<images.length; i++){
var img =images[i].src;
alert(img);
}
}
As @Kyle points out, an img
does not have an href
attribute, they only have src
. Just guessing, but you might have a link (a
) around your images, and its href
stores the path to the big image (in case img
is a thumbnail).
In this situation, you could use:
function checkImages() {
var images = document.images;
for (var i = 0; i < images.length; i++){
if (images[i].parentNode.tagName.toLowerCase() === 'a') {
console.log(images[i].parentNode.href);
}
}
}