Posted in: Events

Inside the Event Handling Function

Every event handling function receives an event object, which contains many properties and methods. The event object is most commonly used to prevent the default action of the event via the .preventDefault() method. However, the event object contains a number of other useful properties and methods, including:

link pageX, pageY

The mouse position at the time the event occurred, relative to the top left of the page.

link type

The type of the event (e.g. "click").

link which

The button or key that was pressed.

link data

Any data that was passed in when the event was bound.

link target

The DOM element that initiated the event.

link preventDefault()

Prevent the default action of the event (e.g. following a link).

link stopPropagation()

Stop the event from bubbling up to other elements.

In addition to the event object, the event handling function also has access to the DOM element that the handler was bound to via the keyword this. To turn the DOM element into a jQuery object that we can use jQuery methods on, we simply do $( this ), often following this idiom:

var elem = $( this );
// Preventing a link from being followed
$( "a" ).click(function( event ) {
var elem = $( this );
if ( elem.attr( "href" ).match( "evil" ) ) {
elem.addClass( "evil" );