JakeP
JakeP

Reputation: 757

How to disable button with jquery?

I've tried everything!

$('#m-button').button('disabled')
$('#m-button').attr('disabled', 'disabled')
$('#m-button').prop('disabled', 'disabled')
$('#m-button').attr('disabled', 'true')
$('#m-button').prop('disabled', 'true')
$('#m-button').attr('enabled', 'false')

<button id="m-button" class="btn" type="button" >Add Note</button>

It stays enabled though. What's the proper way to disable a <button>?

Upvotes: 0

Views: 98

Answers (3)

ebram khalil
ebram khalil

Reputation: 8321

you can try this:

$('#m-button').attr('disabled', 'disabled')

all you need is just to add disabled attribute to your button.

Upvotes: 0

j08691
j08691

Reputation: 207901

$('#m-button').prop('disabled', true)

without quotes around true.

jsFiddle example

Per the jQuery docs on .prop():

Properties generally affect the dynamic state of a DOM element without changing the serialized HTML attribute. Examples include the value property of input elements, the disabled property of inputs and buttons, or the checked property of a checkbox. The .prop() method should be used to set disabled and checked instead of the .attr() method.

Upvotes: 9

dsgriffin
dsgriffin

Reputation: 68596

Remove the quotes from around true and use prop(), like follows:

$('#m-button').prop('disabled', true);

Upvotes: 1

Related Questions