<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.
The disable/enable an input element in jQuery can be done by using prop() method. The prop() method is used to set or return properties and values for the selected elements. Example 1: This example uses prop() method to disable the input text field.
In order to disable an HTML Anchor Link (HyperLink), the value of its HREF attribute is copied to the REL attribute and the value of HREF attribute is set to an empty JavaScript function. This makes HTML Anchor Link (HyperLink) disabled i.e. non-clickable.
It is still possible to disable a link by following 3 steps: remove the href attribute so that it can no longer receive the focus. add a role="link" so that it is always considered a link by screen readers. add an attribute aria-disabled="true" so that it is indicated as being disabled.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With