1

Please can you suggest some sample code on how to activate and deactivate a hyperlink on clicking it.

I tried the following, but no result

1) $("a#click").onclick = function() { return false; }

2) $("a#click").attr ('href', '#');

3)

$(function(){
  $("#disabled a").click(function () { 
    $(this).fadeTo("fast", .5).removeAttr("href"); 
  });
});
THelper
  • 15,333
  • 6
  • 64
  • 104
  • possible duplicate of [how to enable or disable anchor tag using jquery ](http://stackoverflow.com/questions/1164635/how-to-enable-or-disable-anchor-tag-using-jquery) – Alex Angas Nov 11 '10 at 03:15

4 Answers4

3

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

With this code, any clicks on the link will have no effect. Is that what you're looking for?

Rob Knight
  • 8,624
  • 1
  • 20
  • 11
3

i would do it with a css class... if a hyperlink needs to be disabled you toggle its class "disabled" to on.

this gives you the ability to style a.disabled with a different style (cursor, color...)

and in the click event you just check only to perform an action if the clicked link does not own the class 'disabled'

$('a').bind('click', function(){
  if($(this).hasClass('disabled')) {
    // perform actions upon disabled... show the user he cannot click this link
    return false;
  } else {
    // perform actions for the click...
  }
});
Dan Atkinson
  • 11,391
  • 14
  • 81
  • 114
Sander
  • 13,301
  • 15
  • 72
  • 97
2

If you're talking about attaching functionality to an A-tag, but don't want the browser to process the HREF on it, there is a built-in jQuery method to do this:

$("a#click").click(function(event) {
event.preventDefault();

// do stuff here 
 });
Darrell
  • 51
  • 1
0

My possible guess is

$('a').attr('disabled','disabled');

Let us know if it helps..

asyncwait
  • 4,457
  • 4
  • 40
  • 53
  • 2
    this will not work, according to http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-87355129 the disabled attribute on an anchor tag can only be used to trigger stylesheets. -- quote: disabled (of type boolean) Enables/disables the link. This is currently only used for style sheet links, and may be used to activate or deactivate style sheets. -- – Sander Aug 04 '09 at 09:58