178

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?

amirash
  • 2,419
  • 5
  • 24
  • 26
  • 4
    Note that by doing this you cause the values of those elements to "disappear" when submitting the form - to preserve the value make them `readOnly`, not disabled. – Shadow The GPT Wizard Mar 13 '11 at 16:20
  • possible duplicate of [How to disable all div content](http://stackoverflow.com/questions/639815/how-to-disable-all-div-content) – Michel Ayres Aug 13 '13 at 15:08
  • 1
    An Idea could be hide/show a div in front of the controls it allows/prevents from be clicked, plus style disabled controls with css. – Jaider Oct 22 '13 at 22:02

12 Answers12

287

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

$("#parent-selector :input").attr("disabled", true);
Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307
  • can i use this to set all inputs inside div to value 0? – Atom Vayalinkal Jan 04 '12 at 07:59
  • 3
  • 7
    Citing [jQuery](http://api.jquery.com/input-selector/): _Because :input is a jQuery extension and not part of the CSS specification, queries using :input cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. To achieve the best performance when using :input to select elements, first select the elements using a pure CSS selector, then use .filter(":input")._ – acme Sep 12 '12 at 08:16
  • 2
    @acme: Agree 100% but the question doesn't give any hints as to what selector I could possibly filter on. The only information I was given was that the user must be able to do it with a *parent `div` name* and that's it. If given more information I could have come up with a more efficient solution. – Andrew Whitaker Sep 12 '12 at 14:22
  • You're right - I just wanted to mention this for the sake of completeness. Though I guess the performance boost comes only into play with lots of form fields. – acme Sep 12 '12 at 14:29
  • if you want to disable all the elements, not only inputs but some custom buttons, use children(): $('#your-form').children().prop('disabled', true); – walv Jun 25 '14 at 07:27
  • 1
    Use .prop() instead of .attr() (above jquery 1.6) – Akshay Gundewar May 11 '16 at 13:36
  • @acme i tried $("#my_frm").filter(":input").prop('disabled', true); but it's not working (however code in this answer works)...any ideas why? – Andrew Jan 07 '17 at 16:57
  • input not able to handle select (dropdown), need to have one more selector for select: $("#parent-selector :input, #parent-selector select").attr("disabled", true); – John Li Feb 24 '17 at 15:42
  • @Andrew: which jQuery version do you use? – acme Mar 09 '17 at 15:03
  • @acme jquery-1.12.4.min.js and if I remember correctly, it was regarding selection dropdowns – Andrew Mar 09 '17 at 15:50
165
$('#mydiv').find('input, textarea, button, select').attr('disabled','disabled');
Trevor
  • 11,269
  • 2
  • 33
  • 40
  • 4
    it should be ('input, textarea, button') all selectors should be quoted with one set of quotes. http://api.jquery.com/multiple-selector/ Doing it your way you are sending multiple arguments. – Ivan Ivanic Mar 13 '11 at 16:16
  • 2
    This will only find you inputs, ignoring textareas and buttons. The :input selector is the way to go, but if you wanted to list them explicitly, you would need to use `$('#mydiv').find('input, textarea, button')`. – Mark Hildreth Mar 13 '11 at 16:19
  • 1
    ...or just $('#your-form').children().prop('disabled', true); – walv Jun 25 '14 at 07:28
  • @walv your code **only** works if all the form elements are a single descendent of the form. Find() finds all of the elements in the list regardless of descendent level. This line of code is correct other than using the prop instead of attr for new jquery versions. – Chris O Aug 26 '16 at 16:04
33

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

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

or

$("#parent-selector :input").attr("disabled", "disabled");
Optimus Prime
  • 6,817
  • 5
  • 32
  • 60
9

Simply this line of code will disable all input elements

$('#yourdiv *').prop('disabled', true);
Samir Rahimy
  • 2,580
  • 1
  • 18
  • 10
5
    $(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);
        }
    }
Syed Nasir Abbas
  • 1,722
  • 17
  • 11
5

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');
طلحة
  • 375
  • 4
  • 8
1

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");
    }
}
1

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);
irfandar
  • 1,690
  • 1
  • 23
  • 24
0

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;
}
Rohit Dubey
  • 1,234
  • 15
  • 15
0

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.

Melebius
  • 6,183
  • 4
  • 39
  • 52
0

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");
Manish
  • 639
  • 6
  • 17
0

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');
Urvish Joshi
  • 147
  • 8
  • 1
    Please fix your second sentence. It seems to say that your code will disable non-input elements, but then it says it will only affect input elements. Please clarify your meaning. *(Neat trick, by the way - good answer)* – cssyphus Jul 22 '20 at 23:17