Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

disable a hyperlink using jQuery

<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.

like image 969
sarsnake Avatar asked Feb 23 '11 00:02

sarsnake


People also ask

How to disable a using jQuery?

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.

How to disable and enable a link in JavaScript?

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.

How do I disable a link?

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.


1 Answers

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); 
like image 64
David Tang Avatar answered Oct 12 '22 16:10

David Tang