Best way to disable button in Twitter's Bootstrap

user1386320 picture user1386320 · Jun 26, 2013 · Viewed 340.9k times · Source

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?

Answer

Samuel Robert picture Samuel Robert · Jun 26, 2013

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