How to enable or disable an anchor using jQuery?
-
Thanks for all your answer, Still it is not working so can You please make it work with document.ready function – Senthil Kumar Bhaskaran Jul 22 '09 at 12:21
-
It might help if you post the snippet of code that isn't working. – Hooray Im Helping Jul 22 '09 at 12:26
-
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 Jul 23 '09 at 04:28
16 Answers
To prevent an anchor from following the specified href
, I would suggest using preventDefault()
:
// jQuery 1.7+
$(function () {
$('a.something').on("click", function (e) {
e.preventDefault();
});
});
// jQuery < 1.7
$(function () {
$('a.something').click(function (e) {
e.preventDefault();
});
// or
$('a.something').bind("click", function (e) {
e.preventDefault();
});
});
See:
http://docs.jquery.com/Events/jQuery.Event#event.preventDefault.28.29
Also see this previous question on SO:
-
I tried with those "attr" it is working only on form element not on Anchor tag. – Senthil Kumar Bhaskaran Jul 22 '09 at 12:09
-
1@senthil - preventDefault is considered the 'proper' way of doing this, see my edit (link to another Q+A) – karim79 Jul 22 '09 at 12:21
-
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 Jul 23 '09 at 04:30
-
2how do I reverse `$('a.something').on("click", function (e) { e.preventDefault(); });`? – Incerteza Jun 23 '14 at 08:20
-
2Disagree, use CSS "pointer-events: none;" instead, just like in other unswers. – vitrilo Mar 02 '16 at 11:48
-
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;

- 53,070
- 5
- 41
- 38
-
7
-
1Good call, Prestaul. When tied in to an tag, I'm using: .. then: function disableLink(node) { if (dojo.hasClass(node, 'disabled') return false; dojo.addClass(node, 'disabled'); return true; } .. this allows the link to be clicked once and action (return true) and after that it doesn't allow the action (returns false). – Neek Feb 11 '12 at 06:44
-
Note that in the handler you probably should use $(this) instead of $("thelink") since that could change or the user could that way easily copy/paste the code. – Alexis Wilke Mar 09 '14 at 09:47
-
2
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.

- 27,796
- 4
- 47
- 63
Selected Answer is not good.
Use pointer-events CSS style. (As Rashad Annara suggested)
See MDN https://developer.mozilla.org/en-US/docs/Web/CSS/pointer-events. Its supported in most browsers.
Simple adding "disabled" attribute to anchor will do the job if you have global CSS rule like following:
a[disabled], a[disabled]:hover {
pointer-events: none;
color: #e1e1e1;
}

- 1,437
- 16
- 20
-
Also with pointer-events you won't need to include the :hover state as that is a pointer event. You only need include the a[disabled] selector. – Larry Dukek Dec 15 '16 at 07:27
I think a nicer solution is to set disabled data attribute on and anchor an check for it on click. This way we can disable temporarily an anchor until e.g. the javascript is finished with ajax call or some calculations. If we do not disable it, then we can quickly click it a few times, which is undesirable...
$('a').live('click', function () {
var anchor = $(this);
if (anchor.data("disabled")) {
return false;
}
anchor.data("disabled", "disabled");
$.ajax({
url: url,
data: data,
cache: false,
success: function (json) {
// when it's done, we enable the anchor again
anchor.removeData("disabled");
},
error: function () {
// there was an error, enable the anchor
anchor.removeData("disabled");
}
});
return false;
});
I made a jsfiddle example: http://jsfiddle.net/wgZ59/76/

- 5,676
- 6
- 42
- 70
Try the below lines
$("#yourbuttonid").attr("disabled", "disabled");
$("#yourbuttonid").removeAttr("href");
Coz, Even if you disable <a>
tag href
will work so you must remove href
also.
When you want enable try below lines
$("#yourbuttonid").removeAttr("disabled");
$("#yourbuttonid").attr("href", "#nav-panel");

- 3,041
- 4
- 28
- 39

- 107
- 1
- 4
-
1This is exactly what I needed when using JQuery with ASP.net MVC ActionLink. Thanks! – eaglei22 Jan 28 '16 at 16:20
$('#divID').addClass('disabledAnchor');
In css file
.disabledAnchor a{
pointer-events: none !important;
cursor: default;
color:white;
}

- 3,132
- 1
- 25
- 39
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;
})

- 7,639
- 7
- 36
- 54

- 926
- 2
- 9
- 15
$("a").click(function(event) {
event.preventDefault();
});
If this method is called, the default action of the event will not be triggered.

- 27,194
- 17
- 102
- 148

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

- 12,344
- 10
- 72
- 106
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.

- 5,305
- 4
- 33
- 39
For situations where you must put text or html content within an anchor tag, but you simply don't want any action to be taken at all when that element is clicked (like when you want a paginator link to be in the disabled state because it's the current page), simply cut out the href. ;)
<a>3 (current page, I'm totally disabled!)</a>
The answer by @michael-meadows tipped me off to this, but his was still addressing scenarios where you still have to / are working with jQuery/JS. In this case, if you have control over writing the html itself, simply x-ing the href tag is all you need to do, so the solution is a pure HTML one!
Other solutions without jQuery finagling which keep the href require you to put a # in the href, but that causes the page to bounce to the top, and you just want it to be plain old disabled. Or leaving it empty, but depending on browser, that still does stuff like jump to the top, and, it is invalid HTML according to the IDEs. But apparently an a
tag is totally valid HTML without an HREF.
Lastly, you might say: Okay, why not just dump the a tag altogether than? Because often you can't, the a
tag is used for styling purposes in the CSS framework or control you're using, like Bootstrap's paginator:
http://twitter.github.io/bootstrap/components.html#pagination

- 9,104
- 7
- 59
- 69
So with a combination of the responses above, I came up with this.
a.disabled { color: #555555; cursor: default; text-decoration: none; }
$(".yourClass").addClass("disabled").click(function(e){ e.preventDefault();

- 3,469
- 1
- 29
- 32
If you don't need it to behave as an anchor tag then I would prefer to replace it at all. For example if your anchor tag is like
<a class="MyLink" href="http://www.google.com"><span>My</span> <strong>Link</strong></a>
then using jquery you can do this when you need to display text instead of a link.
var content = $(".MyLink").text(); // or .html()
$(".MyLink").replaceWith("<div>" + content + "</div>")
So this way, we can simply replace anchor tag with a div tag. This is much easier (just 2 lines) and semantically correct as well (because we don't need a link now therefore should not have a link)

- 3,123
- 1
- 29
- 25
Disable or Enable any element with the disabled property.
// Disable
$("#myAnchor").prop( "disabled", true );
// Enable
$( "#myAnchor" ).prop( "disabled", false );

- 1,831
- 22
- 21
-
Why is this answer down voted? It answers the question "How to enable or disable an anchor using jQuery?" – RitchieD Mar 08 '18 at 18:46
-
1I believe it is because links still get opened when clicked on the anchor. – Zombaya May 15 '18 at 11:34