I have a link button inside a <td> which I have to disable. This works on IE but not working in Firefox and Chrome.
I tried all the following but not working on Firefox (using 1.4.2 js):
$(".myLink").attr('disabled', 'disabled');
$(".myLink").attr('disabled', true);
$(".myLink").attr('disabled', 'true');
Note – I cannot de-register the click function for the anchor tag as it is registered dynamically. AND I HAVE TO SHOW THE LINK IN DISABLED MODE.
You can’t disable a link (in a portable way). You can use one of these techniques (each one with its own benefits and disadvantages).
CSS way
This should be the right way (but see later) to do it when most of browsers will support it:
It’s what, for example, Bootstrap 3.x does. Currently (2016) it’s well supported only by Chrome, FireFox and Opera (19+). Internet Explorer started to support this from version 11 but not for links however it’s available in an outer element like:
With:
Workaround
We, probably, need to define a CSS class for
pointer-events: nonebut what if we reuse thedisabledattribute instead of a CSS class? Strictly speakingdisabledis not supported for<a>but browsers won’t complain for unknown attributes. Using thedisabledattribute IE will ignorepointer-eventsbut it will honor IE specificdisabledattribute; other CSS compliant browsers will ignore unknowndisabledattribute and honorpointer-events. Easier to write than to explain:Another option for IE 11 is to set
displayof link elements toblockorinline-block:Note that this may be a portable solution if you need to support IE (and you can change your HTML) but…
All this said please note that
pointer-eventsdisables only…pointer events. Links will still be navigable through keyboard then you also need to apply one of the other techniques described here.Focus
In conjunction with above described CSS technique you may use
tabindexin a non-standard way to prevent an element to be focused:I never checked its compatibility with many browsers then you may want to test it by yourself before using this. It has the advantage to work without JavaScript. Unfortunately (but obviously)
tabindexcannot be changed from CSS.Intercept clicks
Use a
hrefto a JavaScript function, check for the condition (or the disabled attribute itself) and do nothing in case.To disable links do this:
To re-enable them:
If you want instead of
.is("[disabled]")you may use.attr("disabled") != undefined(jQuery 1.6+ will always returnundefinedwhen the attribute is not set) butis()is much more clear (thanks to Dave Stewart for this tip). Please note here I’m using thedisabledattribute in a non-standard way, if you care about this then replace attribute with a class and replace.is("[disabled]")with.hasClass("disabled")(adding and removing withaddClass()andremoveClass()).Zoltán Tamási noted in a comment that “in some cases the click event is already bound to some “real” function (for example using knockoutjs) In that case the event handler ordering can cause some troubles. Hence I implemented disabled links by binding a return false handler to the link’s
touchstart,mousedownandkeydownevents. It has some drawbacks (it will prevent touch scrolling started on the link)” but handling keyboard events also has the benefit to prevent keyboard navigation.Note that if
hrefisn’t cleared it’s possible for the user to manually visit that page.Clear the link
Clear the
hrefattribute. With this code you do not add an event handler but you change the link itself. Use this code to disable links:And this one to re-enable them:
Personally I do not like this solution very much (if you do not have to do more with disabled links) but it may be more compatible because of various way to follow a link.
Fake click handler
Add/remove an
onclickfunction where youreturn false, link won’t be followed. To disable links:To re-enable them:
I do not think there is a reason to prefer this solution instead of the first one.
Styling
Styling is even more simple, whatever solution you’re using to disable the link we did add a
disabledattribute so you can use following CSS rule:If you’re using a class instead of attribute:
If you’re using an UI framework you may see that disabled links aren’t styled properly. Bootstrap 3.x, for example, handles this scenario and button is correctly styled both with
disabledattribute and with.disabledclass. If, instead, you’re clearing the link (or using one of the others JavaScript techniques) you must also handle styling because an<a>withouthrefis still painted as enabled.Accessible Rich Internet Applications (ARIA)
Do not forget to also include an attribute
aria-disabled="true"together withdisabledattribute/class.