Posted by: msqr | January 5, 2009

register and pass argument to event handler

Many times we are required to hook event handler with the element through javascript. Main problem we come across during this time is how to pass arguments to registered event handler.

Here’s the handy function that returns Element triggered event handler

function GetEventSource(evt)
{
var ie_var = "srcElement";
var moz_var = "target";

if(window.addEventListener)
{
return evt[moz_var];
}
else
{
return evt[ie_var];
}
}

//Register event handler:
if(window.addEventListener){ // Mozilla, Netscape, Firefox
lnk.addEventListener('click', CallDoPostBackMM, false); }
else { // IE
lnk.attachEvent('onclick', CallDoPostBackMM);
}

function CallDoPostBackMM(evt)
{
var lnk= GetEventSource(evt);
//Now, you have access to lnk element:
//you can retrieve navigateURL, href, set javascript and so on.
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: