JavaScript Function to get Target From Event

Quick one today:

When you’re looking for the target from an event. It’s a good idea to do the following:

[pastacode lang=”javascript” message=”” highlight=”” provider=”manual”]

function stuff(e) {

    var target = e.target || e.srcElement;

}

[/pastacode]

For older IE browsers don’t have “.target” and instead have “srcElement”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.