www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons jQuery UnBinding Events using off() Bookmark and Share
Tutorial Home
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
Lesson 24
Lesson 25
Lesson 26
Lesson 27
We use jQuery on() method to attach code to events of different elements and we use off() method to detach code from the events of different elements.

In the last lesson we learned to use the jQuery on() method to subscribe to events and in this lesson we shall learn to use the off() method to unsubscribe to events.

In our code sometimes we need to subscribe to events, likewise there are times when we would like to do the opposite. We do this with the off() method.

Here is an example:

$ ( "a" ). off ();

The above removes any event handler(s) that we attached to the <a> element using the on() method.

Here is a complete example:

<a href = "javascript:void(0);" onClick="unbindEvent();" > Detach Event </a>
<br>
<a href = "javascript:void(0);" id="exlink" > Click Here </a>

<script type = "text/javascript" >
$ ( function ()
{
$ ( "#exlink" ). on ( "click" , function () {
alert ( $ ( this ). text ());
});
});

function unbindEvent()
{
$ ( "#exlink" ). off ( "click" );
}
</script>

When we run the above example and click on the "Click Here" link it pops up an alert box. Once we click on the "Detach Event" link the "Click Here " link will no more pop up the alert box.

Note: If you are using a jQuery version earlier than 1.7, the off() method will not work. Use unbind() method, instead.

Next >>> Lesson No. 24: jQuery Dynamic Event Handling





Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.