jQuery Event - delegate() Method
Example
Hide or show the p element when the mouse is clicked:
$("div").delegate("button","click",function(){ $("p").slideToggle(); });
Definition and Usage
The delegate() method adds one or more event handlers to the specified elements (which are child elements of the selected elements) and specifies the function to be executed when these events occur.
Event handlers used with the delegate() method are applicable to current or future elements (such as new elements created by scripts).
Syntax
$(selector).delegate(childSelector,event,data,function)
Parameters | Description |
---|---|
childSelector | Required. Specifies one or more child elements to which the event handler is to be attached. |
event |
Required. Specifies one or more events to be attached to the element. Multiple event values separated by spaces. Must be valid events. |
data | Optional. Specifies additional data to be passed to the function. |
function | Required. Specifies the function to be executed when the event occurs. |
More examples
- Add event handlers to future elements
- How to use the delegate() method to add event handlers to elements that have not yet been created.