To start, in jQuery 1.3 event object has been normalized and wrapped into jQuery.Event object. As it says in the documentation: "The event object is guaranteed to be passed to the event handler (no checks for window.event required)."
Here is an jQuery.Event object overview:
- Attributes
- event.type
- event.target
- event.relatedTarget
- event.currentTarget
- event.pageX/Y
- event.result
- event.timeStamp
- Methods
- event.preventDefault()
- event.isDefaultPrevented()
- event.stopPropagation()
- event.isPropagationStopped()
- event.stopImmediatePropagation()
- event.isImmediatePropagationStopped()
Now, how to work with jQuery.Event object?
Anonymous functions that were bind to your elements will receive this new (jQuery.Event) event object and can utilize it's new attributes and methods. So your previous code will work fine (most of the time :) ).
$("a").click(function(event) { alert(event.type); });
The fun part starts when you trigger events with jQuery.Event. You can create new jQuery.Event object and give it to the trigger()
'er to trigger that event.
Example:
// Create new event object // the "new" is optional var e = jQuery.Event("click"); // Add additional data to pass e.user = "foo"; e.pass = "bar"; // Call your event $("a").trigger(e);
NOTE:
You don't have to use new
to create a new jQuery.Event object. It is optional.
Alternative way to pass data through event object:
$("a").trigger({ type:"click", user:"username", pass:"password" });
Try to play with the new event attributes and methods. You can do all kinds of fun things with them. Example: event.result
, event.relatedTarget.
can you give example using jquery event object ?
ReplyDelete@runrunforest, have you looked at an example above?
ReplyDeletePS. Keep tuned, I'll post a new article on jQuery's new event object.