How to dynamically enable / disable links using jQuery?

I have links displayed on the page. I would like to enable / disable them based on other events on the page. Is there a way to do this using jQuery?

+36
jquery hyperlink
Sep 24 '10 at 16:11
source share
6 answers
$('selector_for_links_to_disable').bind('click', function(e){ e.preventDefault(); }) 

and for inclusion:

 $('selector_for_links_to_enable').unbind('click') 
+57
Sep 24 '10 at 16:19
source share

You can do something like:

 $('.links').click(function(e){ if( [some conditions] ){ e.preventDefault(); } }); 

Make sure they no longer work, otherwise your users will be confused, lol.

+4
Sep 24 '10 at 16:16
source share

it depends on what you mean by shutdown.

this will cause them to do nothing:

 $("A").click(function() { return false; }); 
+2
Sep 24 '10 at 16:17
source share
 $(document).delegate('.links', 'click', function () { if ([your condition is true]) { return false; } }) 

delegation is better than handlers, because you can call them before loading dom.

+1
Sep 24 '10 at 16:32
source share

You can do something like this:

 <script> $(document).ready(function() { $('input#disableall').live('click', function(){ $('a').attr( 'class', 'disabled' ); alert('All links are disabled.'); }); $('input#enableall').live('click', function(){ $('a').attr( 'class', 'enabled' ); alert('All links are enabled.'); }); $('a.disabled').live('click', function(event){ event.preventDefault(); }); }); </script> <a href='http://www.google.com'>Google<a/> <a href='http://www.yahoo.com'>Yahoo<a/> <a href='http://www.hotmail.com'>Hotmail<a/> <input type='button' id='disableall' value='Disable Links' /> <input type='button' id='enableall' value='Enable Links' /> 
+1
Sep 24 '10 at 17:16
source share

When I provide functions to buttons on jquery, I like to do this:

 indice = ''; $('myLink').live('click',function() { if (indice !== 'value1'){ // your code } indice = 'value1'; return indice; }); 

with this, you get the function only the first time you press the button. Now you just need to set the difference in value1 so that your link works again.

0
Apr 16 '13 at 21:44
source share



All Articles