Skip to content

jquery_ref_events

jQuery provides a powerful and consistent way to handle browser events. The primary methods for event handling in modern jQuery are .on() to attach handlers and .off() to remove them.

Older methods like .bind(), .delegate(), .live(), and .die() are deprecated and should not be used in new code. The .on() method provides all the functionality of these older methods.

Method / PropertyDescription
.on()Attaches one or more event handlers for the selected elements. This is the recommended method for all event handling, supporting direct and delegated events.
.off()Removes event handlers previously attached with .on().
.one()Attaches an event handler that runs at most once per element per event type.
.trigger()Executes all handlers and behaviors attached to the matched elements for the given event type.
.triggerHandler()Executes all handlers attached to an element for an event, but does not trigger the default action or bubble up the DOM.
Shorthand MethodsMethods like .click(), .hover(), .submit(), .focus(), .blur(), .keydown(), etc., are shorthand for .on('click', ...), .on('mouseenter mouseleave', ...), etc. They are convenient but internally use .on().
Event Object PropertiesProvides information about the event.
event.preventDefault()Prevents the default action of the event (e.g., following a link, submitting a form).
event.stopPropagation()Prevents the event from bubbling up the DOM tree, stopping parent elements from receiving notification of the event.
event.stopImmediatePropagation()Prevents the event from bubbling and also prevents any other handlers on the same element from executing.
event.targetThe DOM element that initiated the event.
event.currentTargetThe DOM element to which the event handler is currently attached (relevant in event bubbling).
event.delegateTargetThe element where the currently-called jQuery event handler was attached (useful in delegated events).
event.typeDescribes the nature of the event (e.g., ‘click’).
event.pageX / event.pageYThe mouse position relative to the top-left corner of the document.
event.whichIndicates the specific key or button that was pressed.
event.dataOptional data object passed to an event method when the current executing handler is bound.
event.namespaceThe namespace specified when the event was triggered (useful for .off() targeting).
event.resultThe last value returned by an event handler that was triggered by this event.
event.timeStampThe time (in milliseconds since Jan 1, 1970) when the event occurred.
event.relatedTargetIn mouse events (like mouseenter/mouseleave), the element being entered or left.
event.isDefaultPrevented()Returns whether event.preventDefault() was called.
event.isPropagationStopped()Returns whether event.stopPropagation() was called.
event.isImmediatePropagationStopped()Returns whether event.stopImmediatePropagation() was called.
Deprecated/Removed Methods(Should not be used) .bind(), .unbind(), .delegate(), .undelegate(), .live(), .die(), .error(), .load(), .unload(), .toggle() (event version).

Event delegation allows you to attach a single event listener to a parent element that will fire for events triggered on its descendants (matching a selector), including descendants added after the listener was attached. This is efficient and useful for dynamic content.

// Example: Handle clicks on list items within a #myList element
// This works even if new <li> elements are added later.
$('#myList').on('click', 'li', function() {
console.log('Clicked on:', $(this).text());
});

Refer to the official jQuery documentation for detailed explanations and examples of each method and event property.