274

I am confused when it comes to disabling a <button>, <input> or an <a> element with classes: .btn or .btn-primary, with JavaScript/jQuery.

I have used a following snippet to do that:

$('button').addClass('btn-disabled');
$('button').attr('disabled', 'disabled');
$('button').prop('disabled', true);

So, if I just provide the $('button').addClass('btn-disabled'); to my element, it will appear as disabled, visually, but the functionality will remain the same and it will be clickable nontheless, so that it the reason why I added the attr and prop settings to the element.

Has anyone expirenced this same issue out there? Is this the right way of doing this - while using Twitter's Bootstrap?

Gottlieb Notschnabel
  • 9,408
  • 18
  • 74
  • 116
  • Could check for hasclass on any attached events, but with an href I'm not sure – Zach Leighton Jun 26 '13 at 18:24
  • @ZachLeighton - when it comes to "a", you have to provide an event handler for click action –  Jun 26 '13 at 18:26
  • I don't think you can disable a link. – Stan Jun 26 '13 at 18:28
  • 2
    I've found that you can in fact disable a link if you bind to a click event on it and call event.preventDefault(). But I wouldn't be surprised if that doesn't work in all browsers. – Mike Turley Aug 27 '13 at 18:00
  • 1
    This was not a duplicate. The duplicate referenced does not cover the case for an "a" href element with classes: "btn" or "btn btn-primary" as stated in the question. – snowleopard Feb 10 '15 at 23:00

6 Answers6

339

You just need the $('button').prop('disabled', true); part, the button will automatically take the disabled class.

Samuel R
  • 4,235
  • 1
  • 13
  • 18
124

For input and button:

$('button').prop('disabled', true);

For anchor:

$('a').attr('disabled', true);

Checked in firefox, chrome.

See http://jsfiddle.net/czL54/2/

ndnenkov
  • 35,425
  • 9
  • 72
  • 104
Jeroen K
  • 10,258
  • 5
  • 41
  • 40
  • 1
    When you add a click handler to disabled anchor link, it should not work. But in your provided example, click handler is also fired. http://jsfiddle.net/76wmpohs/ – ahgindia May 19 '15 at 06:21
  • 1
    This seems to be sufficient for Bootstrap 3. It appears bootstrap is doing something on setting a btn to be visually disabled that also is preventing the click event from firing. In pure jQuery and HTML without Bootstrap 3, such as some of the fiddle examples, it appears to be a more complicated story. But this question was in context of Bootstrap. – Greg Oct 28 '15 at 20:54
90

Building off jeroenk's answer, here's the rundown:

$('button').addClass('disabled'); // Disables visually
$('button').prop('disabled', true); // Disables visually + functionally

$('input[type=button]').addClass('disabled'); // Disables visually
$('input[type=button]').prop('disabled', true); // Disables visually + functionally

$('a').addClass('disabled'); // Disables visually
$('a').prop('disabled', true); // Does nothing
$('a').attr('disabled', 'disabled'); // Disables visually

See fiddle

Community
  • 1
  • 1
Yarin
  • 173,523
  • 149
  • 402
  • 512
  • nice comprehensible example! $('#button-2').attr('disabled', 'disabled') Worked for both buttons as well as links. – msanjay Feb 11 '14 at 14:54
  • 5
    @msanjay: No - the fiddle uses Bootstrap 2 and *visually* disables links/anchors, but doesn't *functionally* disable them, like the comment says. If you change it to BS3 it also functionally disables them. – EML Mar 02 '15 at 17:50
  • EML makes an important point. That in Boot Strap 3, setting the disabled attribute is suffiicent, and Bootrap 3 will handle the rest for you. But in jQuery and HTML (without Bootstrap 3), then such is not sufficient. – Greg Oct 28 '15 at 20:56
38

The easiest way to do this, is to use the disabled attribute, as you had done in your original question:

<button class="btn btn-disabled" disabled>Content of Button</button>

As of now, Twitter Bootstrap doesn't have a method to disable a button's functionality without using the disabled attribute.

Nonetheless, this would be an excellent feature for them to implement into their javascript library.

1234567
  • 946
  • 6
  • 22
31
<div class="bs-example">
      <button class="btn btn-success btn-lg" type="button">Active</button>
      <button class="btn btn-success disabled" type="button">Disabled</button>
</div>
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Vicky
  • 9,515
  • 16
  • 71
  • 88
-1

What ever attribute is added to the button/anchor/link to disable it, bootstrap is just adding style to it and user will still be able to click it while there is still onclick event. So my simple solution is to check if it is disabled and remove/add onclick event:

if (!('#button').hasAttr('disabled'))
 $('#button').attr('onclick', 'someFunction();');
else
 $('#button').removeattr('onclick');
Schone
  • 33
  • 2