Home >>jQuery Tutorial >jQuery delegate() Events

jQuery delegate() Events

jQuery delegate() Events

jQuery delegate() Events in jQuery is used to attaches one or multiple event handlers for specified elements, and specifies a function to run when the events occur.

Syntax:
$(selector).delegate(childSelector,event,data,function)

Parameter Values

Parameter Description
childSelector It is a Required parameter and is used to specifies one or multiple child elements to attach the event handler
event It is a Required parameter and is used to specifies one or multiple events to attach to the elements.
data It is Optional parameter and is used to specifies additional data to pass along to the function
function It is a Required parameter and is used to specifies the function to run when the event occurs
Here is an Example of jQuery delegate() Method:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".Div").delegate(".txt1", "click",function(){
$(".txt1").css("background-color", "orange");
});
});
</script>
</head>
<body>
<div class="Div" style="background-color:green;">
  <p class="txt1">This is first paragraph.</p>
  </div>
<p class="txt1">This is second paragraph.</p>
</body>
</html>

Output:

This is first paragraph.

This is second paragraph.


No Sidebar ads