Help needed with the javascript event model in Internet Explorer
My Super Archive plugin is quite dependent on javascript and uses XMLHttpRequests to fetch the lists that make up the live archive. For each item in the list of years and list of months a function is attached, to run when the item is clicked. In Mozilla based browsers and Safari this is done using the W3C event model and the function addEventListener(), in Internet Explorer I use the attachEvent() functions. So far so good. To find out which item was clicked I rely on the current event being passed into the attached function. This is appearantly part of the W3C event model, and doesn’t work in Internet Explorer, and that is where I’m stumped.
Question: How do I figure out which item was clicked in Internet Explorer, for several items having onclick events attached using attachEvent()?
May 9th, 2005 at 2:31 pm
Hey,
This is a tiny snipped of code from an XMLHttpRequest thing i’m working on for MyBB. Basically this code will return the element upon which an event was triggered upon in both IE and Mozilla based browsers..
Basically I have a variable for isMoz which is just a boolean identifying if the browser is Mozilla. This code will return the element based upon event.currentTarget for Mozilla and event.srcElement for all other browsers.
As far as I can see this works fine..
function eventElement(event)
{
if(isMoz)
{
return event.currentTarget;
}
else
{
return event.srcElement;
}
}
Basically you use it like..
function handleClick(element)
{
textBox.value = eventElement(element).innerHTML;
}
In that example handleClick is the function attached to the onclick handler, and it inserts the HTML (which is just text) for the clicked element in to the textbox.
You should easily be able to adapt it for your own use.
Regards,
Chris