Working with jQuery 1.3's new Event object (jQuery.Event)

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
    1. event.type
    2. event.target
    3. event.relatedTarget
    4. event.currentTarget
    5. event.pageX/Y
    6. event.result
    7. event.timeStamp
  • Methods
    1. event.preventDefault()
    2. event.isDefaultPrevented()
    3. event.stopPropagation()
    4. event.isPropagationStopped()
    5. event.stopImmediatePropagation()
    6. 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.