18

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>
Aman Kumar
  • 4,533
  • 3
  • 18
  • 40
Dhivya
  • 341
  • 1
  • 3
  • 10
  • **Danger**: This HTML is invalid. A ` – Quentin Aug 20 '19 at 09:31

4 Answers4

24

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');
Sahil Sharma
  • 3,847
  • 6
  • 48
  • 98
user3519216
  • 375
  • 2
  • 15
  • 5
    Disabled is not a valid attribute for the anchor tag. – rzb May 17 '18 at 07:08
  • 2
    `$().prop('disabled', true)` is better than `$().attr('disabled', 'disabled')`. See [javascript - .prop() vs .attr()](https://stackoverflow.com/questions/5874652/prop-vs-attr)/5874652/prop-vs-attr – Peter V. Mørch Aug 22 '19 at 11:26
12

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");
Gillu13
  • 898
  • 6
  • 11
  • This is a simple yet effective solution that will work just by having JQuery, and can easily be adapted to vanilla javascript. – S. Dre Mar 10 '22 at 08:20
7

To essentially disable it, you could likely do:

$("a[href='http://www.google.com']").click(function(e) {
  e.preventDefault();
});

But it would be clearer and more precise if you added a class or id and targeted that in the selector. And what this really does is prevent the action from taking place, it doesn't handle management of state, hence making it obvious that the link is not going to work when clicked.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
4

You can something like as follow:

Remove the 'href' attribute from the anchor tag ($target will be your anchor tag):

$target.removeAttr('href');
Juhil Somaiya
  • 873
  • 7
  • 20