Jquery to disable links

5.6k views Asked by At

I have page with several links and linked buttons. I want to disable all. How can I do this?

<div id="content-main">
  <h1>My Notes</h1>

  <a href="edit.html" id="btnNew" name="btnNew" class="button">Add new Link</a>

  <table border="0" cellspacing="0" cellpadding="4">
    <tr bgcolor="#edf3fe">
      <td><a href="edit.html">Link 1</a></td>
      <td><a href="edit.html">Link 2</a></td>
    </tr>
  </table>
  <div class="button-row"></div>
</div>

I tried the following which is working to the point that link no longer works but I would like to fade the links too.

$(document).ready(function(){


    $( "a" ).click(function( event ) {
        alert( "The link will no longer work" );
        event.preventDefault();
    });

}); }

4

There are 4 answers

0
Casey ScriptFu Pharr On BEST ANSWER

If you want them greyed out, then try this. Based on answer above:

$(document).ready(function(){
   $("a").click(function($event){
   var $this = $(this);
       $event.preventDefault();
       $this.css("color", " #808080"); //only targets the actual link clicked. If you want all when you click any link use $('a').css();
   });
});

BTW, word of advice for the newbie, it is always a good practice that I do, by making anything that is an jQuery object variable prefixed with '$'. Easy when you start doing multi-level context functions.

1
John Roca On

You can use this:

$(document).ready(function(){ 
    $( "a" ).click(function( event ) {
       $(this).fadeOut();
       event.preventDefault();
    });
});

Hope this help!

1
Arman Malekzadeh On

how about this ?

$(document).ready(function(){
    $("a").click(function(event){
      event.preventDefault();
        $("a").fadeOut();
    });
});

like what you said, if the user clicks on any of the links, all of them fade out. is this what you want?

0
Jon R On

Not 100% related, but it took me a while to find this trick. If your HTML looks like this

<a id="myLink" href="http://takemesomewhere.com" class="underlineHoverOnly">link text</a>

then you can take the link away with just:

$('#myLink').removeAttr('href');

You can also stop the link underline on hover with:

$('#myLink').removeAttr('class');