views:

317

answers:

3

I am using the following code to disable the click event for anchor tags:

//disable Click event for links except navigation
$("a:not(#navigation a)").live('click', function(e) {
    e.preventDefault;
    return false;
});

I need to reenable or remove the implementation which was set as above on click of an another button.

How to do it in jquery?

+1  A: 

To unbind a .live event you could use .die but I think a better approach would be the following:

$("#buttonToTriggerChange").click(function(e){
    // Toggle between adding and removing the class "disable-links" from
    // the <body> element
    $(document.body).toggleClass('disable-links');
});

$("a:not(#navigation a)").live('click', function(e) {
    // Only block the links if <body> has the class "disabled-links"
    if($(document.body).hasClass('disabled-links')){
        e.preventDefault();
        return false;
    }
});
Doug Neiner
+2  A: 

To remove event handlers connected with live, you can use die:

$("a:not(#navigation a)").die('click');
CMS
A: 

You can kill this with die, according to the jquery docs. You should change your live event handling to a named function rather than an anonymous function:

function live_anchors(e){
    e.preventDefault;
    return false;
}

$("a:not(#navigation a)").live('click', live_anchors);

Then you should be able to undo that with:

$("a:not(#navigation a)").die('click', live_anchors);
Jage
The function reference argument is optional, if it's omitted, all bound handlers will be removed.
CMS