Home > JavaScript Development > IMG, A HREF, and javascript events

IMG, A HREF, and javascript events



Hi,



I have a need to attach event handlers to an anchor surrounding a image. The HTML is something like this:



<a href="http://a.com">

<img src="/images/go.gif"/>

</a>



At the end of the source, I have a piece of javascript that assigns the onclick event to onclick event of the anchor.



When the event handler gets executed as you click on the image on the browser, the 'srcElement' attribute you retrieve from the event object is the image, not the anchor. What I want is to retrieve the href of the anchor. I can't understand why the srcElement is the img though.



I tried to pass an argument to the event, but I don't know how to accomplish that. I cannot find an example on how to pass arguments to an event handler programmatically. The following won't work.



anchors.onclick = function(anchor) {

window.location = anchor;

doLoad();

}





Any advice?



Thanks in advance.

    
Guest


Could we see the code that is returning the image as the target? What's happening in mozilla-based browsers?



btw: LINK

Was this answer helpful ? Yes No   
Guest


Hi,



Thanks for the reply. The HTML source is exactly that. I have attached this javascript at the end of the HTML file. Sample2.js is the one I'm using now, but the srcElement will be the image. That is why I'm experimenting with Sample.js, but I just couldn't pass the variable into the function.



Thanks

Matthew

Was this answer helpful ? Yes No   
Guest


Whoa. Your original question was pretty generic; what you posted is much more involved. Hard to play with it without some idea of exactly what you need to achieve. A complete demo - HTML as well, doesn't need to be elaborate - would be very helpful. I assume know some urls are already parsed out into their components by the system....



http://www.xs4all.nl/~ppk/js/events_order.html

Was this answer helpful ? Yes No   
Guest
 
 
Home - About Infoqu - Contact - Privacy Statement - Link to Infoqu - Bookmark Infoqu

Copyright 2007-2010 by Infoqu. All rights reserved