What is the use of delegate() method in jQuery ?
The delegate() Method in jQuery is used to add event handlers to the element that are children of selected elements. When the event occurs then the function will be run. This will work for current and future elements (if we want to create some elements later).
$(selector) .delegate(childSelector, event, data, function)
Parameters: This function accepts four parameters.
- childSelector: This is a required parameter and it is used to specify the children to attach the event handler.
- event: This is a required parameter and it is used to specify the events to attach to the elements. If there are multiple event values then they will be separated by space.
- data: This is an optional parameter and it is used to pass the extra data using the function.
- function: This is a required parameter and it specifies the function to run when the event occurs.
Example: The following code demonstrates the delegate() method in jQuery.