jQuery off() remove handler by function

Description

jQuery off() remove handler by function

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  w w.  j a  va  2 s. c o m*/
<script>
function changeSize() {
  $(this).animate({fontSize: "+=10px"});
}

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

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

<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<p>Click any p element to increase size and letter spacing.</p>

<button>Remove the changeSize() event handler</button>

</body>
</html>



PreviousNext

Related