I am assigning an event handler function to an element through the native browser onclick property:
document.getElementById('elmtid').onclick = function(event) { anotherFunction(event) };
When I'm in anotherFunction(event)
, I want to be able to use the event object like I would with the event object you get in jQuery through the .on()
method. I want to do this because the jQuery event object has properties and methods such as .pageX
, .pageY
and .stopPropagation()
that work across all browsers.
So my question is, after I've passed in the native browser event object into anotherFunction()
, how can I turn it into a jQuery event? I tried $(event)
, but it didn't work.
The obvious question here is: why don't you just use jQuery .on
, .bind
, .click
etc to assign your event handling functions? The answer: I'm building a page that has a huge table with lots of clickable things on it. Unfortunately this project requires that the page MUST render quickly in IE6 and IE7. Using .on
et al in IE6 and IE7 creates DOM leaks and eats up memory very quickly (test for yourself with Drip: http://outofhanwell.com/ieleak/index.php?title=Main_Page). Setting onclick behavior via .onclick
is the only option I have to render quickly in IE6 and IE7.
$.event.fix(event)
- which is what jQuery does internally. The keyword being "internally", meaning you shouldn't assume it will still work the same way in the next version of jQuery. – Expectantnew jQuery.Event(nativeEvent)
. I don't know if it will work. requires 1.6 or later. – Canonicateevent.pageX
doesn't work, then maybe tryevent.originalEvent.pageX
. I haven't tried this myself so I am not 100% sure of it. – WouldjQuery.Event()
doesn't normalize anything at all, contrary to what the docs imply (but don't actually state). – ExpectantjQuery.Event()
is for creating synthetic events (typically prior to.trigger()
), not for native event normalization. – Roveover