Alex Rivera | Logout

Best way to get the Original Target

Asked 2009-01-27T21:50:00.927
30

What's a jQuery like and/or best practices way of getting the original target of an event in jQuery (or in browser javascript in general).

I've been using something like this

$('body').bind('click', function(e){
        //depending on the browser, either srcElement or 
        //originalTarget will be populated with the first
        //element that intercepted the click before it bubbled up
        var originalElement = e.srcElement;
        if(!originalElement){originalElement=e.originalTarget;}                         
});

which works, but I'm not pleased with the two line feature sniffing. Is there a better way?

Edit
Report

1 Answer

0

in normal Javascript, var t = (e.originalTarget)?e.originalTarget:e.srcElement; should be enough to read it across all browsers.

answered 2011-06-27T11:20:21.063

Your Answer