How to disable all <input > inside a form with jQuery?

JqueryForms

Jquery Problem Overview


<form id="target">
....
</form>

Jquery Solutions


Solution 1 - Jquery

In older versions you could use attr. As of jQuery 1.6 you should use prop instead:

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

To disable all form elements inside 'target'. See :input:

> Matches all input, textarea, select and button elements.

If you only want the <input> elements:

$("#target input").prop("disabled", true);

Solution 2 - Jquery

Above example is technically incorrect. Per latest jQuery, use the prop() method should be used for things like disabled. See their API page.

To disable all form elements inside 'target', use the :input selector which matches all input, textarea, select and button elements.

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

If you only want the elements, use this.

$("#target input").prop("disabled", true);

Solution 3 - Jquery

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)

Solution 4 - Jquery

you can add

 <fieldset class="fieldset">

and then you can call

 $('.fieldset').prop('disabled', true);

Solution 5 - Jquery

With this one line you can disable any input field in a form

$('form *').prop('disabled', true);

Solution 6 - Jquery

To disable all form, as easy as write:

jQuery 1.6+

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

jQuery 1.5 and below

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

Solution 7 - Jquery

You can do it like this:

//HTML BUTTON
<button type="button" onclick="disableAll()">Disable</button>

//Jquery function
function disableAll() {
    //DISABLE ALL FIELDS THAT ARE NOT DISABLED
    $('form').find(':input:not(:disabled)').prop('disabled', true);

    //ENABLE ALL FIELDS THAT DISABLED
    //$('form').find(':input(:disabled)').prop('disabled', false);
}

Solution 8 - Jquery

The definitive answer (covering changes to jQuery api at version 1.6) has been given by Gnarf

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionomgView Question on Stackoverflow
Solution 1 - JquerycletusView Answer on Stackoverflow
Solution 2 - JqueryMetaSkillsView Answer on Stackoverflow
Solution 3 - JqueryMetaSkillsView Answer on Stackoverflow
Solution 4 - JqueryOtto KanellisView Answer on Stackoverflow
Solution 5 - JquerySamir RahimyView Answer on Stackoverflow
Solution 6 - JqueryAngel CuencaView Answer on Stackoverflow
Solution 7 - JqueryKenan BegićView Answer on Stackoverflow
Solution 8 - JqueryErichBSchulzView Answer on Stackoverflow