Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 582
PeakJi Avatar asked Jun 23 '11 09:06

PeakJi


1 Answers

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.

like image 116
Variant Avatar answered Sep 26 '22 12:09

Variant