jQuery bind()Method
Examples
Add a click event to <p> elements:
$("p").bind("click",function(){
alert("This paragraph was clicked.");
});
alert("This paragraph was clicked.");
});
Try it Yourself »
Definition and Usage
The bind() method adds one or more event handlers to selected elements, as well as the function to run when the event occurs.
As of jQuery version 1.7,on()the method is the preferred way to add event handlers to selected elements.
Syntax
$(selector).bind(event,data,function,map)
| Parameter | Description |
|---|---|
| event | Required. Specifies one or more events to add to the element. Multiple event values separated by spaces. Must be valid events. |
| data | Optional. Specifies additional data to pass to the function. |
| function | Required. Specifies the function to run when the event occurs. |
| map | Specifies an event map ({event:function, event:function, ...}), containing one or more events to add to the element, and the function to run when the event occurs. |
More Examples
Add multiple events
How to add multiple events to an element.
Using an event map
How to use an event map to add some events/functions to selected elements.
Pass data to the function
How to pass data to a custom-named event handler.
Other Extensions
jQuery Event Methods