jquery_ref_events
jQuery Event Handling
Section titled “jQuery Event Handling”Modern jQuery Event Methods
Section titled “Modern jQuery Event Methods”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.
Key Methods & Concepts:
Section titled “Key Methods & Concepts:”| Method / Property | Description |
|---|---|
| .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 Methods | Methods 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 Properties | Provides 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.target | The DOM element that initiated the event. |
| event.currentTarget | The DOM element to which the event handler is currently attached (relevant in event bubbling). |
| event.delegateTarget | The element where the currently-called jQuery event handler was attached (useful in delegated events). |
| event.type | Describes the nature of the event (e.g., ‘click’). |
| event.pageX / event.pageY | The mouse position relative to the top-left corner of the document. |
| event.which | Indicates the specific key or button that was pressed. |
| event.data | Optional data object passed to an event method when the current executing handler is bound. |
| event.namespace | The namespace specified when the event was triggered (useful for .off() targeting). |
| event.result | The last value returned by an event handler that was triggered by this event. |
| event.timeStamp | The time (in milliseconds since Jan 1, 1970) when the event occurred. |
| event.relatedTarget | In 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 with .on()
Section titled “Event Delegation with .on()”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.