Search code examples
jquerydisable-link

Jquery to disable links


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();
    });

}); }


Solution

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