views:

203

answers:

8

Hello all,

I have the below which disables all hyperlinks but after an event I want to enable them back all again, how can I do this?

$("a").click(function() { return false; });

I don't think its as simple as just setting it to true. ;)

Thanks all

A: 

You want to unbind the event: http://api.jquery.com/unbind/

DA
A: 

You should be able to unbind it.

$("a").unbind("click");
Shawn Steward
+5  A: 

Instead of binding your "click" handler that way, do this:

$('a').bind("click.myDisable", function() { return false; });

Then when you want to remove that handler it's easy:

$('a').unbind("click.myDisable");

That way you avoid messing up other stuff that might be bound to "click". If you just unbind "click", you unbind everything bound to that event.

Pointy
Thank you for that tip - I don't want to mess anything else up!
Abs
does binding a function that returns false stop any other bound handlers from executing after it? Don't you also need to stop the event propagation?
Mike Sherov
This is sort-of "cargo cult" but when I really want to kill the event I do: return !!event.stopPropagation()
Pointy
A: 
var disableLink = function(){ return false;};
$('a').bind('click', disableLink);

to restore:

$('a').unbind('click', dsiableLink);
prodigitalson
A: 

you could unbind the click handler:

$('a').unbind('click')
Patricia
+1  A: 

Try this:

// Better to use the live event handler here, performancewise
$('a').live('click', function() {... return false;});

// Now simply kill the binding like this
$('a').die('click');

bye

aefxx
+1  A: 

Binding and unbinding takes some overhead.

A different solution would be to add a class like 'disabled', then use hasClass('disabled') to test and see whether or not it should return 'false'.

$('a').addClass('disabled');

$('a').click(function() {
    if($(this).hasClass('disabled')
        return false;
});
patrick dw
A: 
$(function(){


    $.myStopAnchor = function(){
      $stop = true;
    }

    $.myNoStopAnchor = function(){
      $stop = false;
    }

    $.myNoStopAnchor();

    $("a").click(function(ev) { 
       if ($stop){
         ev.stopPropagation()
       }
       return !$stop; 
    });

});
andres descalzo