How to disable or enable a form element with jQuery?

by Yang Yang on April 16, 2009

Share This Article:
Subscribe to Kavoir: blog feed

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");

Share This Article:
Subscribe to Kavoir: blog feed

You should also read:

{ 5 comments… read them below or add one }

Andrea January 3, 2010 at 5:54 pm

Ty very much!

Reply

Bashar April 30, 2010 at 2:01 am

Very useful, thanks.

Reply

Ken Collins July 13, 2011 at 4:02 am

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)

Reply

Wade August 1, 2011 at 9:51 am

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

Reply

faeb187 November 10, 2011 at 7:22 pm

@wade: native javascript is always faster than the jquery way.. in your case use: document.getElementById( “checkboxfield” ).disabled = true…

Reply

Leave a Comment

Previous post:

Next post: