Skip navigation

If you’re going to hardcode event handlers right into your HTML, (perhaps its being delivered from a php page) there’s 2 ways you can do this:

<span onclick=” runFuncAndTargetIsTHIS( this ); “> <!– way A –>

or:

<span onclick=” runFuncButNeedToExtractTarget( event ); “> <!– way B –>

Function for way A looks like:

runFuncAndTargetIsTHIS( elem )
{
    alert( elem + ' is the HTML element that was clicked' );
}

While way B:

runFuncButNeedToExtractTarget( e )
{
    e = e || window.event;
    var target = e.target || e.srcElement;
    alert( target + ' is the HTML element that was clicked' );
}

Some people say you shouldn’t do things using Way A. But I find Way A sometimes convenient. The only reason I can think of right now that Way A is bad is its easier to cancel propagation (cancel bubbling) of event registered using way B.

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

%d bloggers like this: