Best way to disable button in Twitter's Bootstrap [duplicate]
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?
Solution 1:
You just need the $('button').prop('disabled', true);
part, the button will automatically take the disabled class.
Solution 2:
For input and button:
$('button').prop('disabled', true);
For anchor:
$('a').attr('disabled', true);
Checked in firefox, chrome.
See http://jsfiddle.net/czL54/2/
Solution 3:
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
Solution 4:
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.