Categories
JavaScript Tips & Tutorials jQuery Tips & FAQ

How to disable or enable a form element with jQuery?

You need 2 jQuery functions: attr and removeAttr.

To disable a form element such as a text input or a button (with a made-up id: #elm):

$("#elm").attr("disabled", "disabled");

To enable a disabled form element:

$("#elm").removeAttr("disabled");

By Yang Yang

Hello, I'm Yang. I build online businesses that please people. Want to join in and post some useful articles on Kavoir.com? Shoot me a message.

7 replies on “How to disable or enable a form element with jQuery?”

The jQuery docs say to use prop() for things like disabled, checked, etc. Also the more concise way is to use their selectors engine. So to disable all form elements in a div or form parent.

$myForm.find(‘:input:not(:disabled)’).prop(‘disabled’,true);

And to enable again.

$myForm.find(‘:input:disabled’).prop(‘disabled’,false)

If you have one field to change, uou can also do it with $(‘#checkboxfield’)[0].disabled = true

Comments are closed.