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.
prop() method is used to disable all input controls inside a form element.
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.
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 .
$("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 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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With