jQuery event.delegateTarget

Introduction

Change the background color of the <div> element.

View in separate window

<!DOCTYPE html>
<html>
<head>
<script 
 src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>//  ww  w. jav a 2s .  c  o  m
<script>
$(document).ready(function(){
  $("div").on("click", "button", function(event){
    $(event.delegateTarget).css("background-color", "pink");
  });
});
</script>
</head>
<body>

<div style="background-color:yellow">
  <p>Click the button to change the background color of this div.</p>
  <button>Click me!</button>
</div>

<div style="background-color:yellow">
  <p>Click the button to change the background color of this div.</p>
  <button>Click me!</button>
</div>

</body>
</html>

The event.delegateTarget property returns the element for the jQuery event.

event.delegateTarget
Parameter OptionalDescription
event Required.The event parameter comes from the event binding function



PreviousNext

Related