62
<a href="gohere.aspx" class="my-link">Click me</a>

I did

$('.my-link').attr('disabled', true);

but it didn't work

Is there an easy way to disable the hyperlink using jquery? Remove href? I would rather not fiddle with href. So if I can do it w/o removing href, that would be great.

sarsnake
  • 26,667
  • 58
  • 180
  • 286
  • possible duplicate of [How do I dynamically enable/disable links with jQuery?](http://stackoverflow.com/questions/3788946/how-do-i-dynamically-enable-disable-links-with-jquery) – Ashish Jul 06 '15 at 14:10

11 Answers11

126

You can bind a click handler that returns false:

$('.my-link').click(function () {return false;});

To re-enable it again, unbind the handler:

$('.my-link').unbind('click');

Note that disabled doesn't work because it is designed for form inputs only.


jQuery has anticipated this already, providing a shortcut as of jQuery 1.4.3:

$('.my-link').bind('click', false);

And to unbind / re-enable:

$('.my-link').unbind('click', false);
Vikrant
  • 4,920
  • 17
  • 48
  • 72
David Tang
  • 92,262
  • 30
  • 167
  • 149
  • 5
    With 1.4.3 or later, you can shorten it a bit with `$('.my-link').bind('click',false);`. – user113716 Feb 23 '11 at 00:11
  • 4
    As of jQuery 1.7, `bind()` and `unbind()` have been replaced with `on()` and `off()`. See the [docs](http://api.jquery.com/unbind/) for more info. – Sir Celsius Aug 21 '14 at 14:57
  • 1
    [this](http://stackoverflow.com/a/17935158/2218697) post has **easiest way using bootstrap**, hope helps. – Shaiju T Oct 15 '16 at 16:25
32

I know it's an old question but it seems unsolved still. Follows my solution...

Simply add this global handler:

$('a').click(function()
{
     return ($(this).attr('disabled')) ? false : true;
});

Here's a quick demo: http://jsbin.com/akihik/3

you can even add a bit of css to give a different style to all the links with the disabled attribute.

e.g

a[disabled]
{
    color: grey; 
}

Anyway it seems that the disabled attribute is not valid for a tags. If you prefer to follow the w3c specs you can easily adopt an html5 compliant data-disabled attribute. In this case you have to modify the previous snippet and use $(this).data('disabled').

Luciano Mammino
  • 798
  • 8
  • 23
  • 1
    I think this should actually be: if ($(this).attr('disabled')) { e.stopImmediatePropagation(); } – Jules Jun 14 '15 at 10:41
  • 2
    Also, to ensure that dynamically added anchors are also disabled, the full code should be: $("body").on("click", "a", function (e) { if ($(this).attr('disabled')) { e.stopImmediatePropagation(); } }); – Jules Jun 14 '15 at 10:53
  • 1
    (can't edit comment after 5 mins). I forgot to add: e.preventDefault(); – Jules Jun 14 '15 at 11:01
12

Removing the href attribute definitely seems to the way to go. If for some reason you need it later, I would just store it in another attribute, e.g.

$(".my-link").each(function() {
    $(this).attr("data-oldhref", $(this).attr("href"));
    $(this).removeAttr("href");
});

This is the only way to do it that will make the link appear disabled as well without writing custom CSS. Just binding a click handler to false will make the link appear like a normal link, but nothing will happen when clicking on it, which may be confusing to users. If you are going to go the click handler route, I would at least also .addClass("link-disabled") and write some CSS that makes links with that class appear like normal text.

Riley Dutton
  • 7,615
  • 2
  • 24
  • 26
  • removing the `href` will also mess up SEO rankings. leaving the attribute and disabling it will make robots realise it is a link. – Sir Celsius Aug 21 '14 at 14:55
8
$('.my-link').click(function(e) { e.preventDefault(); }); 

You could use:

$('.my-link').click(function(e) { return false; }); 

But I don't like to use this myself as it is more cryptic, even though it is used extensively throughout much jQuery code.

user229044
  • 232,980
  • 40
  • 330
  • 338
Neil
  • 3,229
  • 1
  • 17
  • 15
7

The pointer-events CSS property is a little lacking when it comes to support (caniuse.com), but it's very succinct:

.my-link { pointer-events: none; } 
tiffon
  • 5,040
  • 25
  • 34
  • But this can also disable any other work the link does. For example, the hover won't work. – m4n0 Sep 25 '20 at 08:00
4
function EnableHyperLink(id) {
        $('#' + id).attr('onclick', 'Pagination("' + id + '")');//onclick event which u 
        $('#' + id).addClass('enable-link');
        $('#' + id).removeClass('disable-link');
    }

    function DisableHyperLink(id) {
        $('#' + id).addClass('disable-link');
        $('#' + id).removeClass('enable-link');
        $('#' + id).removeAttr('onclick');
    }

.disable-link
{
    text-decoration: none !important;
    color: black !important;
    cursor: default;
}
.enable-link
{
    text-decoration: underline !important;
    color: #075798 !important;
    cursor: pointer !important;
}
Aleksander Blomskøld
  • 18,374
  • 9
  • 76
  • 82
2

Append a class containing pointer-events:non

.active a{ //css
text-decoration: underline;
background-color: #fff;
pointer-events: none;}


$(this).addClass('active');
Andrien Pecson
  • 282
  • 3
  • 13
2

The disabled attribute isn't valid on all HTML elements I believe, see the MSDN article. That and the proper value for disabled is simply "disabled". Your best approach is to bind a click function that returns false.

wsanville
  • 37,158
  • 8
  • 76
  • 101
1

Below will replace the link with it's text

$('a').each(function () {
    $(this).replaceWith($(this).text());
});

Edit :

Above given code will work with hyperlinks with text only, it will not work with images. When we'll try it with image link it won't show any image.

To make this code compatible with image links following will work fine

// below given function will replace links with images i.e. for image links
$('a img').each(function () {
    var image = this.src;
    var img = $('<img>', { src: image });
    $(this).parent().replaceWith(img);
});

// This piece of code will replace links with its text i.e. for text links
$('a').each(function () {
    $(this).replaceWith($(this).text());
});

explanation : In above given code snippets, in first snippet we are replacing all the image links with it's images only. After that we are replacing text links with it's text.

IT ppl
  • 2,626
  • 1
  • 39
  • 56
  • Thank you so much! You just saved me from having to edit 2400 chunks of text... or learn regexes (and who wants to do that, right?). – Kyle Carlson Jul 21 '13 at 12:47
  • 1
    How can I set the link back to its default? suppose I disabled all the links with $(this).replaceWith($(this).text()); but how can I set it to its enabled default? – sasha Nov 15 '13 at 13:24
0

This also works well. Is simple, lite, and doesn't require jQuery to be used.

<a href="javascript:void(0)">Link</a>
Bradley Flood
  • 10,233
  • 3
  • 46
  • 43
-3

Try:

$(this).prop( "disabled", true );
Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95
user670265
  • 483
  • 4
  • 3