16

What is the nicest way to disable all form controls on a web page? I have read that this cannot be done via CSS but this would be my preferred option if possible. I also have access to jQuery if that makes life easier.

Chris
  • 26,744
  • 48
  • 193
  • 345
  • Possible dupe http://stackoverflow.com/questions/2310816/disabling-all-form-elements-in-selected-div-edited and http://stackoverflow.com/search?q=disable+form – zaf Apr 26 '10 at 08:34
  • This has nothing to do with presentation, so CSS would be the wrong tool. (It also sounds like you may be trying to get things that look like form controls, but never intend to get user input using them, if so, then form controls are the wrong tool too.) – Quentin Apr 26 '10 at 08:36
  • @Chris - ideally the update to your question should be asked separately. – karim79 Apr 26 '10 at 08:40

3 Answers3

20
$("form :input").attr("disabled","disabled");

:input selects all form controls, including input, textarea, select and button elements.

If you want to disable form elements that are outside of forms too, simply omit the 'form` selector from the above.

karim79
  • 339,989
  • 67
  • 413
  • 406
2

@karim79 is right with his CSS selectors, but convention calls for the jquery .prop() function (see docs here).

So I suggest...

$("form :input").prop("disabled",true);

Here is a similar situation where the .prop() function is appropriate.

Hope this helps!

Community
  • 1
  • 1
Freestyle076
  • 1,548
  • 19
  • 36
1

U can disable all input type areas using below one.

jQuery 1.5 and below

any text box - $("input[type=text]").attr('disabled', true);
any radio button - $("input[type=radio]").attr('disabled', true);
any checkbox - $("input[type=checkbox]").attr('disabled',true);

jQuery 1.6+

any text box - $("input[type=text]").prop('disabled', true);
any radio button - $("input[type=radio]").prop('disabled', true);
any checkbox - $("input[type=checkbox]").prop('disabled',true);

Kasun Gamage
  • 346
  • 2
  • 3
  • 18