jQuery off() remove an event handler

Description

jQuery off() remove an event handler

View in separate window

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Remove Event Handler</title>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script>
function sayHello(){/*  w  ww. j a v a 2s . c  o m*/
    alert("Hello, World!");
}
$(document).ready(function(){
    // Attach an event handler function to click event
    $("#btnOn, #btnOff").on("click", sayHello);

    // Removes the click event handler from second button
    $("#btnOff").off("click");
});
</script>
</head>
<body>
    <button type="button" id="btnOn">Click On</button>
    <button type="button" id="btnOff">Click Off</button>
</body>
</html>



PreviousNext

Related