views:

9014

answers:

8

Please Help how to enable or disable the anchor tag using jquery

+2  A: 
$("a").click(function(){
                alert('disabled');
                return false;

});
Alexander Corotchi
+20  A: 

To prevent an anchor from following the specified HREF, I would suggest using preventDefault():

$(document).ready(function() {
    $('a.something').click(function(e) {
        e.preventDefault();
    });
});

See:

http://docs.jquery.com/Events/jQuery.Event#event.preventDefault.28.29

Also see this previous question on SO:

http://stackoverflow.com/questions/970388/jquery-disable-a-link/970413#970413

karim79
I tried with those "attr" it is working only on form element not on Anchor tag.
Senthil Kumar Bhaskaran
@senthil - preventDefault is considered the 'proper' way of doing this, see my edit (link to another Q+A)
karim79
Actually My coding is in Rails and my coding is <%= foo.add_associated_link('Add email', @project.email.build) %> when it I render it into browser i can see the email but i cannot disabled it even i tried with coding such as e.preventDefault() but of no result
Senthil Kumar Bhaskaran
+1  A: 

If you are trying to block all interaction with the page you might want to look at the jQuery BlockUI Plugin

Sam Hasler
+1  A: 

You never really specified how you wanted them disabled, or what would cause the disabling.

First, you want to figure out how to set the value to disabled, for that you would use JQuery's Attribute Functions, and have that function happen on an event, like a click, or the loading of the document.

Sneakyness
+9  A: 

The app I'm currently working on does it with a CSS style in combination with javascript.

a.disabled { color:gray; }

Then whenever I want to disable a link I call

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

Then, in the click handler for 'thelink' a tag I always run a check first thing

if ($('thelink').hasClass('disabled')) return;
AgileJon
This is definitely the best answer here! Great job.
rp
Shouldn't that last line say "return false;"?
Prestaul
+1  A: 

i worked with all the samples given above, but still i am unable to disable the link tag

but i could hide the link with this code: $('a.add').hide();

similarly, please suggest some other way to disable the link tag

Are you sure that the code is even working? Try making something else happen like changing the color of something. Check the event log in your browser for errors.
Sneakyness
A: 

It's as simple as return false;

ex. jQuery("li a:eq(0)").click(function(){ return false; })

or

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

kim edgard
+1  A: 

I found an answer that I like much better here

Looks like this:

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

Enabling would involve setting the href attribute

$(document).ready(function(){
    $("a").click(function () { 
        $(this).fadeIn("fast").attr("href", "http://whatever.com/wherever.html"); 
    });
});

This gives you the appearance that the anchor element becomes normal text, and vice versa.

Michael Meadows
Nice answer thanks buddy
Senthil Kumar Bhaskaran