2

I have a hyper link on my web page and I want to disable so the user cannot click on it and when the user do changes in other part of the form (an event happen) the link gets enabled.

<a href="" id="saveChanges" >Save changes</a>

I tried $("#saveChanges").attr("disabled", true); but I still can click on the link. any idea? thanks

user2870274
  • 63
  • 1
  • 2
  • 7
  • Can we see your code ? – Denys Séguret Nov 09 '13 at 18:34
  • I am guessing you attached a click handler to that anchor tag to 'save changes' and you don't want it to fire when you 'disable' it? What stops you from checking the disabled attribute of the anchor tag in the click handler? – Sumurai8 Nov 10 '13 at 12:11

2 Answers2

1

An anchor isnt a form field that can be disabled through an HMTL declartion (which by the way should be using the .prop() instead of .attr()).

It has to be done step by step.

$(function () {
    $('a').on("click", function (e) {
        e.preventDefault();
    });
});
Zevi Sternlicht
  • 5,399
  • 19
  • 31
1

To make a link unclickable you may remove the href attribute. However before you remove the value of href you should store it in another attribute in other to restore it if required. For example,

function makeUnClickable() {
    $('a').each(function () {
        $(this).data('href', $(this).attr('href'));
        $(this).removeAttr('href');
    });
}
function makeClickable() {
    $('a').each(function () {
        $(this).attr('href', $(this).data('href'));
    });
}

http://jsfiddle.net/cC7Um/

melc
  • 11,523
  • 3
  • 36
  • 41
  • Interesting idea, but what if there are many hyperlinks you would like to disable/enable? – carmenism Sep 30 '14 at 19:25
  • @vulpix it works for multiple links as it is, this example makes it clearer http://jsbin.com/xoyeqejusepi/1 . Also added a selector in case it is required to disable/enable a specific link. – melc Oct 01 '14 at 06:17