Search code examples
javascripthtmlioshrefsrc

Getting the href attribute of an image with Javascript


New to Javascript, really need some help!

Now I have an image in a HTML page, like this:

<a class="p" href="http://www.abc.com"><img src="http://www.abc.com/logo.jpg" alt="" /></a>

And get the image element by:

var e.document.elementFromPoint(x,y);

When I clicked on the image, I can get the src attribute or offset attributes successfully by:

e.src or e.offsetHeight

However, it returns NULL when I use:

return e.href;

So how can I get the correct href attribute (http://www.abc.com) ??

Thanks,

Peak


Solution

  • The href is not a propery of the image but of the A element.

    You can acces it by using the .parentNode propery of the image. as it is its direct parent.