Capturing events in IE

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Capturing events in IE

Post by kendall »

Ok... No I have been researching and I see that IE is a real pain in the ass. I have a html markup

Code: Select all

 
<a href=""><img src=""/></a>
 
and I'm using an addEventListener/ attachEvent on to the <a> for a click event. Now while FF/ Opera and Chrome gives me the <a> tag....IE is giving me the <img> tag

I have used the whole cancelBubble = true and returnValue = false but I can't seem to figure out how to get the <a> in IE (7).

anyone with some insight workarounds?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Capturing events in IE

Post by pickle »

I don't think you want to cancel the bubble - bubbling is what's firing the event in the first place.

It's hard to give solid advice without seeing more detail, but maybe check the event closer & see if there's some aspect of the event that maps to the <a>. It's entirely possible that there's one property that maps to what triggered the event (the image) and one property that maps to what acted on the event (the <a>).
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Capturing events in IE

Post by JAB Creations »

This should work just fine in IE...

Code: Select all

<element onclick="alert(this.id);" />
...then replace alert with a custom function...and alert the id...

Code: Select all

<element onclick="my_function(event);" />
...if IE is giving you the img element then use the W3C DOM to access it's parentNode. :wink:

Good tutorial for JavaScript nodes here: http://www.howtocreate.co.uk/tutorials/ ... /dombasics
spider.nick
Forum Commoner
Posts: 72
Joined: Wed Jul 15, 2009 12:22 pm
Location: Overland Park, KS

Re: Capturing events in IE

Post by spider.nick »

JAB Creations wrote:This should work just fine in IE...

Code: Select all

<element onclick="alert(this.id);" />
...then replace alert with a custom function...and alert the id...

Code: Select all

<element onclick="my_function(event);" />
...if IE is giving you the img element then use the W3C DOM to access it's parentNode. :wink:

Good tutorial for JavaScript nodes here: http://www.howtocreate.co.uk/tutorials/ ... /dombasics
But, and this is only preference at this point, separating out your JavaScript from your HTML is a better practice.

So, in your JavaScript file, you might do something like this:

Code: Select all

 
window.onload = function() {
var a_tags = document.getElementsByClassName('class_name');
 
// For legit browsers
a_tags.addEventListener( 'click', functionName , false );
 
// For IE
a_tags.attachEvent( 'onclick', functionName )
}
 
Nick
Post Reply