391

Although that link is disabled, it's still clickable.

<a href="/" disabled="disabled">123n</a>

Can I make it not-clickable if it's disabled? Should I use JavaScript necessarily?

Alan Coromano
  • 24,958
  • 53
  • 135
  • 205

30 Answers30

512

With the help of css you will disable the hyperlink. Try the below

a.disabled {
  pointer-events: none;
  cursor: default;
}
<a href="link.html" class="disabled">Link</a>
brk
  • 48,835
  • 10
  • 56
  • 78
Lakhan
  • 12,328
  • 3
  • 19
  • 28
308

There is no disabled attribute for hyperlinks. If you don't want something to be linked then you'll need to remove the <a> tag altogether.

Alternatively you can remove its href attribute - though this has other UX and Accessibility issues as noted in the comments below so is not recommended.

Barry Pollard
  • 40,655
  • 7
  • 76
  • 92
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • 27
    This works, but I would consider it bad UX if you leave the existing CSS on it. The user will click it and think something is broken. You should never allow the user to become confused about what is happening. – agm1984 May 11 '18 at 06:49
  • Just adding cursor: none; to the relevant link seems to achieve as much as cursor: none; pointer-events: none; . . . . At any rate it's enabling a change of page in a single-page app site for me. – Trunk Jan 21 '19 at 20:51
  • 2
    The link would still be focusable and "clickable" (using Enter) using keyboard navigation even if you use CSS cursor or pointer-events, which is probably bad if you're trying to disable something Either remove the href attribute or turn it into a span – tbjgolden Apr 13 '19 at 22:50
  • 5
    You can also add `onclick="return false;"` to the anchor tag and perhaps add a `title` attribute that explains that the link is not valid. – Craig London Jun 18 '19 at 17:52
  • 1
    Also, I believe simply clearing out the `href` and leaving the `` would not be ADA compliant. – SlothFriend Oct 10 '19 at 17:08
  • If you are using PHP, you can echo out the href along side the disabled attribute E.g, if ($status == approved){echo 'disabled= "" href = "#" '}. Very crude but it works. – Asuquo12 Nov 15 '19 at 08:23
101

You can use:

<a href="/" onclick="return false;">123n</a>
CodeLikeBeaker
  • 20,682
  • 14
  • 79
  • 108
85

You can use one of these solutions:

HTML

<a>link</a>

JavaScript

<a href="javascript:function() { return false; }">link</a>
<a href="/" onclick="return false;">link</a>

CSS

<a href="www.page.com" disabled="disabled">link</a>

<style type="text/css">
    a[disabled="disabled"] {
        pointer-events: none;
    }
</style>

Blowsie
  • 40,239
  • 15
  • 88
  • 108
IgniteCoders
  • 4,834
  • 3
  • 44
  • 62
45

Try this:

<a href="javascript:void(0)" style="cursor: default;">123n</a>
23

The <a> tag doesn't have a disabled attribute, that's just for <input>s (and <select>s and <textarea>s).

To "disable" a link, you can remove its href attribute, or add a click handler that returns false.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • @rocket Are there any implications of removing the href attribute? I mean, it's supposed to be a mandatory attribute. Not that that sort of thing seems to matter much nowadays. – Ringo Oct 27 '16 at 21:29
  • @Ringo: Removing the `href` may make the cursor not change when you hover over it. – gen_Eric Oct 27 '16 at 21:35
23

HTML:

<a href="/" class="btn-disabled" disabled="disabled">123n</a>

CSS:

.btn-disabled,
.btn-disabled[disabled] {
  opacity: .4;
  cursor: default !important;
  pointer-events: none;
}
Syed
  • 15,657
  • 13
  • 120
  • 154
17

Tips 1: Using CSS pointer-events: none;

Tips 2: Using JavaScript javascript:void(0) (This is a best practice)

<a href="javascript:void(0)"></a>

Tips 1: Using Jquery $('selector').attr("disabled","disabled");

venkatskpi
  • 800
  • 6
  • 8
15

You need to remove the <a> tag to get rid of this.

or try using :-

  <a href="/" onclick="return false;">123n</a>
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
7

Letting a parent have pointer-events: none will disable the child a-tag like this (requires that the div covers the a and hasn't 0 width/height):

<div class="disabled">
   <a href="/"></a>
</div>

where:

.disabled {
    pointer-events: none;
}
Ferus
  • 1,080
  • 3
  • 12
  • 17
6
$(document).ready(function() {
   $('a').click(function(e) {
     e.preventDefault();
  });
});
Mahmoud Farahat
  • 5,364
  • 4
  • 43
  • 59
6

You can emulate the disabled attribute on a <a> tag.

<a href="link.html" disabled="">Link</a>

a[disabled] {
   pointer-events: none;
   cursor: default;
}
Blowsie
  • 40,239
  • 15
  • 88
  • 108
5

you can disable link using javascript at run time by using this code

$('.page-link').css("pointer-events", "none");

  • Late to the party, but this also removes any cursor css. In my case, I use the "cursor: not-allowed" if a link is disabled. – Steve Mar 22 '19 at 10:33
5

I have one:

<a href="#">This is a disabled tag.</a>

Hope it will help you ;)

4

If you want to get rid of the pointer you can do this with css using cursor.

ahdaniels
  • 1,050
  • 1
  • 12
  • 25
4

In my case, I use

<a href="/" onClick={e => e.preventDefault()}>
4

We can't disable it directly but we can do the following:

  1. add type="button".
  2. remove the href="" attribute.
  3. add disabled attribute so it shows that it's disabled by changing the cursor and it becomes dimmed.

example in PHP:

<?php
if($status=="Approved"){ 
?>
  <a type="button" class="btn btn-primary btn-xs" disabled> EDIT
     </a>
  <?php    
}else{
?>
  <a href="index.php" class="btn btn-primary btn-xs"> EDIT
    </a>
  <?php    
}
?>
gekkedev
  • 562
  • 4
  • 18
Assaad Lutf
  • 57
  • 1
  • 3
3

I was able to achieve the desired result using an ng-href ternary operation

<a ng-href="{{[condition] ? '' : '/'}}" ng-class="{'is-disabled':[condition]}">123n</a>

where

a.is-disabled{
   color: grey;
   cursor: default;

   &:hover {
      text-decoration: none;
   }
} 
3

The best answer is always the simplest. No need for any coding.

If the (a) anchor tag has no href attribute, it is only a placeholder for a hyperlink. Supported by all browsers!

<a href="/">free web counters</a><br  />
<a "/">free web counters</a>
2

Use buttons and links correctly.

• Buttons activate scripted functionality on a web page (dialogs, expand/collapse regions).

• Links take you to another location, either to a different page or different location on the same page.

If you follow these rules there won't be any scenario when you need to disable a link. Even if you make a link look visually disabled and blank onclick

<a href="" ng-click="Ctrl._isLinkActive && $ctrl.gotoMyAwesomePage()"

You won't be considering accessibility and screen readers will read it as a link and visually impaired people will keep wondering why is the link not working.

Amit
  • 680
  • 3
  • 10
2

Here are various ways to disable the a tag :

<a >Link Text</a> remove the href from your anchor tag
<a href=”javascript:void(0)”/>Link text</a> put javascript:void(0) in href
<a href="/" onclick="return false;">Link text</a> using return false

As there is no disabled attributes for anchor tag. If you want to stop anchor tag behavior inside a jQuery function than you can use the the below line in the start of the function :

$( "a" ).click(function( e ) {
e.preventDefault();
$( "<div>" )
.append( "default " + e.type + " prevented" )
.appendTo( "#log" );
});

event.preventDefault()

Mahak Choudhary
  • 1,286
  • 1
  • 16
  • 13
2

I see there are already a ton of answers posted here, but I don’t think there’s any clear one yet that combines the already mentioned approaches into the one I’ve found to work. This is to make the link both appear disabled, and also not redirect the user to another page.

This answer assumes you’re using jquery and bootstrap, and uses another property to temporarily store the href property while disabled.

//situation where link enable/disable should be toggled
function toggle_links(enable) {
    if (enable) {
        $('.toggle-link')
        .removeClass('disabled')
        .prop('href', $(this).attr('data-href'))
    }
    else {
        $('.toggle-link')
        .addClass('disabled')
        .prop('data-href', $(this).prop('href'))
        .prop('href','#')
    }
a.disabled {
  cursor: default;
}
owengall
  • 455
  • 4
  • 11
1

If you're using WordPress, I created a plugin that can do this & much more without needing to know how to code anything. All you need to do is add the selector of the link(s) that you want to disable & then choose "Disable all links with this selector in a new tab." from the dropdown menu that appears and click update.

Click here to view a gif that demonstrates this

You can get the free version from the WordPress.org Plugin repository to try it out.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
1

If you need to disabled link on laravel with Javascript, here is my solution:

Link(blade.php):

<a href='/link/to/path' class='btn btn-primary mt-3' onclick='disableLink(this)'>Confirmar</a>

.css file

.isDisabled {
    cursor: not-allowed;
    opacity: 0.5;
}

a[aria-disabled="true"] {
    color: currentColor;
    display: inline-block; /* For IE11/ MS Edge bug */
    pointer-events: none;
    text-decoration: none;
}

.js file

function disableLink(link) {
    // 1. Add isDisabled class to parent element
    link.parentElement.classList.add('isDisabled');
    // 2. Store href so we can add it later
    link.setAttribute('data-href', link.href);
    // 3. Set aria-disabled to 'true'
    link.setAttribute('aria-disabled', 'true');
}

function enableLink(link) {
    // 1. Remove 'isDisabled' class from parent span
    link.parentElement.classList.remove('isDisabled');
    // 2. Set href
    link.href = link.getAttribute('data-href');
    // 3. Remove 'aria-disabled', better than setting to false
    link.removeAttribute('aria-disabled');
}

Reference: https://css-tricks.com/how-to-disable-links/

0

Anything in the href tag will display at the bottom-left of the browser window when you mouse over it.

I personally think having something like javascript:void(0) displayed to the user is hideous.

Instead, leave href off, do your magic with with jQuery/whatever else and add a style rule (if you still need it to look like a link):

a {
    cursor:pointer;
}
Enigma Plus
  • 1,519
  • 22
  • 33
-1

Variant that suits me:

<script>
  function locker(){
    if ($("a").hasClass("locked")) {
         $("a").removeClass('locked').addClass('unlocked');
         $("a").attr("onClick","return false");
    } else {
         $("a").css("onclick","true");
         $("a").removeClass('unlocked').addClass('locked');
         $("a").attr("onClick","");
    }
  }
</script>

<button onclick="locker()">unlock</button>

<a href="http://some.site.com" class="locked">
  <div> 
      .... 
  <div>
</a>
Lewis86
  • 511
  • 6
  • 15
Litter
  • 127
  • 1
  • 2
-1

You can disable anchor tags by returning false. In my case Im using angular and ng-disabled for a Jquery accordion and I need to disable the sections.

So I created a little js snippet to fix this.

       <a class="opener" data-toggle="collapse"
                   data-parent="#shipping-detail"
                   id="shipping-detail-section"
                   href="#shipping-address"
                   aria-expanded="true"
                   ng-disabled="checkoutState.addressSec">
                   Shipping Address</a>
     <script>
          $("a.opener").on("click", function () {
           var disabled = $(this).attr("disabled");
           if (disabled === 'disabled') {
            return false;
           }
           });
     </script>
-1

if you just want to temporarily disable the link but leave the href there to enable later (which is what I wanted to do) I found that all browsers use the first href. Hence I was able to do:

<a class="ea-link" href="javascript:void(0)" href="/export/">Export</a>
Angus Walker
  • 378
  • 3
  • 13
  • This is invalid HTML, as described at https://stackoverflow.com/q/26341507/1709587. If your motivation is just to keep the href around in your source code so it's easy to edit your source later to restore it, there are ways of doing that that don't involve violating the spec, like using a comment. I'd recommend against writing invalid HTML like this without a solid reason for doing so. – Mark Amery Dec 08 '18 at 15:40
-1

<a href="/" disabled="true" onclick="return false">123</a>

Just adding: This works in general, however it wont work if user has disabled javascript in browser.

1) You could optionally use Bootstrap 3 class on your anchor tag to disable the href tag, after integrating bootstrap 3 plugin do

<a href="/" class="btn btn-primary disabled">123n</a>

Or

2) Learn how to enable javascript using html or js in browsers. or create a pop-up telling user to enable javascript using before using the website

OlaJ
  • 608
  • 7
  • 15
-3

There is an easy and clean way like so:

<a href="/shopcart/">

  <button class="btn" disabled> Make an Order </button>

</a>

Having disabled attribute on a <button> by default does not let clicks go through <button> element up to <a> element. So <a> element does not even know that some clicks happened. Manipulate by adding/removing disabled attribute on a <button>.