Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to disable anchor tag in jquery?

Here,How to disable this link by using button tab?Is it possible?

 <a href="http://www.google.com">
    <button>click here for go to yahoo</button>
</a>
like image 828
Dhivya Avatar asked Apr 30 '14 10:04

Dhivya


People also ask

Can you disable an anchor tag?

To disable a HTML anchor element with CSS, we can apply the pointer-events: none style. pointer-events: none will disable all click events on the anchor element. This is a great option when you only have access to class or style attributes. It can even be used to disable all the HTML links on a page.

How can make anchor tag not clickable in jQuery?

Answer: Use the jQuery removeAttr() method You can easily remove the clickable behavior from a link through removing the href attribute from the anchor element ( <a> ) using the jQuery removeAttr() method.

How can I check whether the anchor tag is disabled or not in jQuery?

$('#buttononclickdisable'). click(function(){ $('#disableanchor'). attr("disabled","disabled"); });

How do I stop my anchor from clicking?

To prevent an anchor from visiting the specified href, you can call the Event interface's preventDefault() method on the anchor's click handle.


2 Answers

Give ids and disable at the run-time.

HTML:

<a id="disableanchor" href="http://www.google.com">
        <button id="buttononclickdisable">click here for go to yahoo</button>
</a>

Javascript:

$('#buttononclickdisable').click(function(){
     $('#disableanchor').attr("disabled","disabled");
});

Or remove the 'click' event listener.

$("#anchorid").off('click');
like image 142
user3519216 Avatar answered Sep 17 '22 17:09

user3519216


Surprisingly it is not such a straightforward task since anchor tags do not have a disabled attribute (on the contrary of input tags, select tags, textarea tags, etc.).

The workaround I often use is first to define a class with pointer-events set to none (CSS):

.disable-click{
    pointer-events:none;
}

Then I use Jquery (1.0+) to disable/enable the "clickability" of the anchor tag in question by adding/removing the class previously defined:

$("#my-a-tag-id").addClass("disable-click");
$("#my-a-tag-id").removeClass("disable-click");
like image 39
Gillu13 Avatar answered Sep 19 '22 17:09

Gillu13