views:

1197

answers:

4

suggest me some sample codes to activate and deactivate a hyperlink on clicking it.

i tried some of these, but no result

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

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

3) $(#document).ready(function(){ $("#disabled a").click(function () { $(this).fadeTo("fast", .5).removeAttr("href"); }); });

so, please suggest some other code

+2  A: 

$("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
A: 

My possible guess is

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

Let us know if it helps..

Vadi
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
+2  A: 

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...
}
});
Sander
A: 

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