disable all form elements inside div

JqueryFormsHtmlField

Jquery Problem Overview


is there a way to disable all fields (textarea/textfield/option/input/checkbox/submit etc) in a form by telling only the parent div name in jquery/javascript?

Jquery Solutions


Solution 1 - Jquery

Try using the :input selector, along with a parent selector:

$("#parent-selector :input").attr("disabled", true);

Solution 2 - Jquery

$('#mydiv').find('input, textarea, button, select').attr('disabled','disabled');

Solution 3 - Jquery

For jquery 1.6+, use .prop() instead of .attr(),

$("#parent-selector :input").prop("disabled", true);

or

$("#parent-selector :input").attr("disabled", "disabled");

Solution 4 - Jquery

    $(document).ready(function () {
        $('#chkDisableEnableElements').change(function () {
            if ($('#chkDisableEnableElements').is(':checked')) {
                enableElements($('#divDifferentElements').children());
            }
            else {
                disableElements($('#divDifferentElements').children());
            }
        });
    });

    function disableElements(el) {
        for (var i = 0; i < el.length; i++) {
            el[i].disabled = true;

            disableElements(el[i].children);
        }
    }

    function enableElements(el) {
        for (var i = 0; i < el.length; i++) {
            el[i].disabled = false;

            enableElements(el[i].children);
        }
    }

Solution 5 - Jquery

Simply this line of code will disable all input elements

$('#yourdiv *').prop('disabled', true);

Solution 6 - Jquery

How about achieving this using only HTML attribute 'disabled'

<form>
 <fieldset disabled>
  <div class="row">
   <input type="text" placeholder="">
   <textarea></textarea>
   <select></select>
  </div>
  <div class="pull-right">
    <button class="button-primary btn-sm" type="submit">Submit</button>
  </div>
 </fieldset>
</form>

Just by putting disabled in the fieldset all the fields inside of that fieldset get disabled.

$('fieldset').attr('disabled', 'disabled');

Solution 7 - Jquery

I'm using the function below at various points. Works in a div or button elements in a table as long as the right selector is used. Just ":button" would not re-enable for me.

function ToggleMenuButtons(bActivate) {
    if (bActivate == true) {
        $("#SelectorId :input[type='button']").prop("disabled", true);
    } else {
        $("#SelectorId :input[type='button']").removeProp("disabled");
    }
}

Solution 8 - Jquery

For me the accepted answer did not work as I had some asp net hidden fields which got disabled as well so I chose only to disable visible fields

//just to save the list so we can enable fields later
var list = [];
$('#parent-selector').find(':input:visible:not([readonly][disabled]),button').each(function () {
    list.push('#' + this.id);
});

$(list.join(',')).attr('readonly', true);

Solution 9 - Jquery

Following will disable all the input but will not able it to btn class and also added class to overwrite disable css.

$('#EditForm :input').not('.btn').attr("disabled", true).addClass('disabledClass');

css class

.disabledClass{
  background-color: rgb(235, 235, 228) !important;
}

Solution 10 - Jquery

Use the CSS Class to prevent from Editing the Div Elements

CSS:

.divoverlay
{
position:absolute;
width:100%;
height:100%;
background-color:transparent;
z-index:1;
top:0;
}

JS:

$('#divName').append('<div class=divoverlay></div>');

Or add the class name in HTML Tag. It will prevent from editing the Div Elements.

Solution 11 - Jquery

Only text type

$(".form-edit-account :input[type=text]").attr("disabled", "disabled");

Only Password type

$(".form-edit-account :input[type=password]").attr("disabled", "disabled");

Only Email Type

$(".form-edit-account :input[type=email]").attr("disabled", "disabled");

Solution 12 - Jquery

If your form inside div simply contains form inputting elements, then this simple query will disable every element inside form tag:

<div id="myForm">
	<form action="">
    ...
    </form>
</div>

However, it will also disable other than inputting elements in form, as it's effects will only be seen on input type elements, therefore suitable majorly for every type of forms!

$('#myForm *').attr('disabled','disabled');

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
QuestionamirashView Question on Stackoverflow
Solution 1 - JqueryAndrew WhitakerView Answer on Stackoverflow
Solution 2 - JqueryTrevorView Answer on Stackoverflow
Solution 3 - JqueryOptimus PrimeView Answer on Stackoverflow
Solution 4 - JquerySyed Nasir AbbasView Answer on Stackoverflow
Solution 5 - JquerySamir RahimyView Answer on Stackoverflow
Solution 6 - JqueryطلحةView Answer on Stackoverflow
Solution 7 - JqueryAWizardInDallasView Answer on Stackoverflow
Solution 8 - JqueryirfandarView Answer on Stackoverflow
Solution 9 - JqueryRohit DubeyView Answer on Stackoverflow
Solution 10 - JqueryLaddu VasanthView Answer on Stackoverflow
Solution 11 - JqueryManishView Answer on Stackoverflow
Solution 12 - JqueryUrvish JoshiView Answer on Stackoverflow