jQuery undelegate() remove event by element, event name and handle function name

Introduction

Click any p element to increase size and letterspacing.

Remove the event handler changeSize() for all p elements

View in separate window

<!DOCTYPE html>
<html>
<head>
<script 
 src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>/*from  w ww  .  j ava 2  s.  c  o m*/
<script>
function changeSize() {
  $(this).animate({fontSize: "+=3px"});
}

function changeSpacing() {
  $(this).animate({letterSpacing: "+=2px"});
}

$(document).ready(function(){
  $("body").delegate("p", "click", changeSize);
  $("body").delegate("p", "click", changeSpacing);
  $("button").click(function(){
       $("body").undelegate("p", "click", changeSize);
  });
});
</script>
</head>
<body>

<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

<button>Test</button>

</body>
</html>



PreviousNext

Related