` elements: ```javascript $(document).ready(function(){ $("p").on("click", function(){ alert("The paragraph was clicked."); }); }); ``` ### 2. Attaching Multiple Event Handlers You can bind multiple events to the same element. #### Using Space-Separated Events: ```javascript // Triggers the same function for both mouseenter and mouseleave $("p").on("mouseenter mouseleave", function(){ $(this).toggleClass("highlight"); }); ``` #### Using an Object Map: You can pass an object to attach different functions to different events: ```javascript $("p").on({ mouseenter: function(){ $(this).css("background-color", "lightgray"); }, mouseleave: function(){ $(this).css("background-color", "lightblue"); }, click: function(){ $(this).css("background-color", "yellow"); } }); ``` ### 3. Event Delegation (For Dynamic/Future Elements) By using the `childSelector` parameter, you can attach an event handler to a parent element that will react to events triggered by its current or **future** child elements. ```javascript // This works even for
Event On
## jQuery `on()` Method
The `on()` method attaches one or more event handlers for the selected elements and their child elements.
Since jQuery version 1.7, the `on()` method is the modern replacement for the older `bind()`, `live()`, and `delegate()` methods. It brings consistency to the jQuery API and simplifies the codebase. It is highly recommended to use `on()` for all event binding in modern jQuery applications.
* **Note:** Event handlers attached using the `on()` method will work for both current elements and **future elements** (such as new elements created dynamically by a script).
* **Tip:** To remove event handlers attached with `on()`, use the [`off()`](event-off.html) method.
* **Tip:** To attach an event handler that runs only once and then removes itself, use the [`one()`](event-one.html) method.
---
## Syntax
```javascript
$(selector).on(event, childSelector, data, function)
```
### Parameter Values
| Parameter | Description |
| :--- | :--- |
| `event` | **Required.** Specifies one or more space-separated event types or namespaces (e.g., `"click"`, `"focus mouseover"`). Must be a valid event. |
| `childSelector` | **Optional.** A selector string to filter the descendants of the selected elements that trigger the event. Used for **event delegation** (replacing the deprecated `delegate()` method). |
| `data` | **Optional.** Data to be passed to the handler function in `event.data` when an event is triggered. |
| `function` | **Optional.** The function to execute when the event is triggered. |
---
## Code Examples
### 1. Basic Event Binding
Attach a simple `click` event handler to all ` elements added to the list dynamically later
$("ul").on("click", "li", function(){
$(this).css("background-color", "pink");
});
```
### 4. Passing Custom Data to the Event Handler
You can pass data to the event handler function using the `data` parameter, which is accessed via `event.data`:
```javascript
function greet(event) {
alert("Hello " + event.data.name);
}
$("button").on("click", { name: "Alice" }, greet);
```
### 5. Custom Namespace Events
Namespaces allow you to bind and unbind specific event handlers without affecting others of the same event type:
```javascript
// Bind a click event with a custom namespace "myNamespace"
$("p").on("click.myNamespace", function(){
alert("Custom namespace event triggered.");
});
// Trigger the namespaced event manually
$("p").trigger("click.myNamespace");
// Unbind only the namespaced click event
$("p").off("click.myNamespace");
```
---
## Legacy Replacements (Upgrading to `on()`)
If you are refactoring older jQuery code, here is how to translate legacy event methods to the modern `on()` syntax:
### Replacing `bind()`
* **Old Syntax:** `$("p").bind("click", function(){ ... });`
* **New Syntax:** `$("p").on("click", function(){ ... });`
### Replacing `delegate()`
* **Old Syntax:** `$("div").delegate("p", "click", function(){ ... });`
* **New Syntax:** `$("div").on("click", "p", function(){ ... });` *(Note the swapped order of the event and selector parameters)*
### Replacing `live()`
* **Old Syntax:** `$("p").live("click", function(){ ... });`
* **New Syntax:** `$(document).on("click", "p", function(){ ... });` *(Delegates the event to the document root)*
YouTip