How to enable or disable an anchor using jQuery?

Senthil Kumar Bhaskaran picture Senthil Kumar Bhaskaran · Jul 22, 2009 · Viewed 310.7k times · Source

How to enable or disable an anchor using jQuery?

Answer

karim79 picture karim79 · Jul 22, 2009

To prevent an anchor from following the specified href, I would suggest using preventDefault():

// jQuery 1.7+
$(function () {
    $('a.something').on("click", function (e) {
        e.preventDefault();
    });
});

// jQuery < 1.7
$(function () {
    $('a.something').click(function (e) {
        e.preventDefault();
    });

    // or 

    $('a.something').bind("click", function (e) {
        e.preventDefault();
    });
});

See:

http://docs.jquery.com/Events/jQuery.Event#event.preventDefault.28.29

Also see this previous question on SO:

jQuery disable a link