jQuery off() method
The off() method of jQuery is used to remove event handlers attached to specified elements. This can be used to remove event handlers attached with any method like by using on(), animate() etc.
Syntax of off() method
$(selector).off(event , event_handler )
e.g.
$(“div”).off(“click” , event_handler )
Running example of using off() method
In example below, when you click “Double click to test bind method with on() method!” an alert will be shown by using on() method. After clicking button below that text, if you again double click above text, alert will not be shown.
Experience this example online
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
<html>
<head>
<script src=“http://code.jquery.com/jquery-1.10.1.min.js”></script>
<script>
$(document).ready(function(){
$(“#bindtest”).bind(“dblclick”,function(){
alert(“The Div is double clicked.”);
});
$(“button”).click(function(){
$(“#bindtest”).off(“dblclick”);
});
});
</script>
</head>
<body>
<div id=“bindtest”>Double click to test bind method with on() method!</div>
<button>Click to remove handler by using Off() method</button>
</body>
</html>
|
Leave A Comment?