Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable all form controls on a web page

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.

like image 743
Chris Avatar asked Apr 26 '10 08:04

Chris


People also ask

How do I disable all form fields?

prop() method is used to disable all input controls inside a form element.

How do you make a whole form Disabled in HTML?

Disabling an entire <form> in HTML You cannot change disabled input fields or use disabled buttons. But the form element itself does not have such a disabled attribute. It is therefore not possible to disable a complete HTML form including all its descendants directly.

How do I turn off form elements?

Disabling all form elements HTML form elements have an attribute called disabled that can be set using javascript. If you are setting it in HTML you can use disabled="disabled" but if you are using javascript you can simply set the property to true or false .


2 Answers

$("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.

like image 197
karim79 Avatar answered Oct 27 '22 03:10

karim79


@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!

like image 40
Freestyle076 Avatar answered Oct 27 '22 04:10

Freestyle076