Cancel the click event of tag a

2

I need to create a JavaScript function that is called at the click of a button and should cancel the click of a <a> tag.

I tried this way:

$("#botao").click(function(){
  $("a").unbind('click')
})

However, when you click on the tag, it will continue sending you the link of your href attribute. I've already tried with .off('click') , but also with no success.

After canceling the click, you need to be able to go back with the click event.

    
asked by anonymous 13.12.2016 / 22:01

2 answers

0

Rersolvi inserting a date attribute to the link that if true continues with the get snag does a return false.

    
14.12.2016 / 14:51
2

You can use jQuery to get this result, follow the example.

Click once and remove the link, click again and add the link.

$( ".remover-link" ).click(function() {
  $( ".link" ).each(function( i ) {
    if ($(this).attr("href") == "google.com") {
      $(this).removeAttr( "href" );
    } else {
     $(this).attr( "href", "google.com");
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><aclass="link" href="google.com">Google.com</a><br>
<button class="remover-link">Remover Link</button>

I find it a bit less complex then using pure javascript.

    
13.12.2016 / 22:29