82

I am looking for a method to Enable and Disable the div id="dcalc" and Its children.

<div id="dcalc" class="nerkheArz"
 style="left: 50px; top: 150px; width: 380px; height: 370px;
 background: #CDF; text-align: center" >
 <div class="nerkh-Arz"></div>
 <div id="calc"> </div>
</div>

I want to Disable them at loading the page and then by a click i can enable them ?

This is what i have tried

document.getElementById("dcalc").disabled = true;
3
  • 3
    What is the difference between an "enabled" and "disabled" div element? It is really just input elements that can be disabled. Or do you want to disable all child input elements within a div? Commented Dec 7, 2011 at 22:57
  • I want to disable every divs that are under main one
    – Emax
    Commented Dec 8, 2011 at 22:22
  • Please visit: stackoverflow.com/questions/639815/… Commented Feb 20, 2013 at 5:53

3 Answers 3

188

You should be able to set these via the attr() or prop() functions in jQuery as shown below:

jQuery (< 1.7):

// This will disable just the div
$("#dcacl").attr('disabled','disabled');

or

// This will disable everything contained in the div
$("#dcacl").children().attr("disabled","disabled");

jQuery (>= 1.7):

// This will disable just the div
$("#dcacl").prop('disabled',true);

or

// This will disable everything contained in the div
$("#dcacl").children().prop('disabled',true);

or

//  disable ALL descendants of the DIV
$("#dcacl *").prop('disabled',true);

Javascript:

// This will disable just the div
document.getElementById("dcalc").disabled = true;

or

// This will disable all the children of the div
var nodes = document.getElementById("dcalc").getElementsByTagName('*');
for(var i = 0; i < nodes.length; i++){
     nodes[i].disabled = true;
}
13
  • 5
    .prop('disabled', true) would be better. And your last code block leaks a global (you need var i). Commented Dec 7, 2011 at 23:19
  • 1
    If you are already using JQuery - then I recommend using it. However, if you aren't the Javascript solution will be much easier to implement. As you will just have to wrap those in a <script> block. (These kinds of things are what jQuery really excels at) Commented Dec 8, 2011 at 20:59
  • 14
    It doesn't work. I still can click the children links. Commented Jan 9, 2014 at 9:22
  • 6
    Me too, it doesn't work. I still can click the children links Commented Feb 28, 2014 at 13:13
  • 7
    NOTE that $el.children() only traverses a single level down the DOM, so it will NOT in fact disable ALL descendants of the DIV. So, @Emerald214 @oliver-pons, to accomplish this, you'd have to do something like $("#dcac1 *").prop("disabled", true); Otherwise, great answer.
    – Todd
    Commented Oct 28, 2014 at 22:32
57

If you want to disable all the div's controls, you can try adding a transparent div on the div to disable, you gonna make it unclickable, also use fadeTo to create a disable appearance.

try this.

$('#DisableDiv').fadeTo('slow',.6);
$('#DisableDiv').append('<div style="position: absolute;top:0;left:0;width: 100%;height:100%;z-index:2;opacity:0.4;filter: alpha(opacity = 50)"></div>');
6
  • 3
    Combine it with suppressing keyboard and copy/paste events: var stopPropFn = function (e) { e.stopPropagation(); e.preventDefault(); }; $('#DisableDiv').bind("keydown", stopPropFn); $('#DisableDiv').bind("keypress", stopPropFn); $('#DisableDiv').bind("paste", stopPropFn); Commented Jul 23, 2013 at 18:22
  • 1
    This doesn't prevent the user going to the links or inputs with the "tab" key where they can still interact with them. Commented Dec 25, 2013 at 17:31
  • 2
    You should give the masking division an ID so you can delete it when you want to re-enable the division
    – bobbdelsol
    Commented Oct 31, 2014 at 0:27
  • 1
    How do you undo it ? I want this to be like a toggle.
    – Oliver
    Commented Sep 11, 2018 at 11:07
  • @oliver give the div thats being appended an id such as "overlay". Then when you want to remove simply use $("#overlay").remove();
    – Jake
    Commented Feb 7, 2019 at 2:26
23

The following selects all descendant elements and disables them:

$("#dcacl").find("*").prop("disabled", true);

But it only really makes sense to disable certain element types: inputs, buttons, etc., so you want a more specific selector:

$("#dcac1").find(":input").prop("disabled",true);
// noting that ":input" gives you the equivalent of
$("#dcac1").find("input,select,textarea,button").prop("disabled",true);

To re-enable you just set "disabled" to false.

I want to Disable them at loading the page and then by a click i can enable them

OK, so put the above code in a document ready handler, and setup an appropriate click handler:

$(document).ready(function() {
    var $dcac1kids = $("#dcac1").find(":input");
    $dcac1kids.prop("disabled",true);

    // not sure what you want to click on to re-enable
    $("selector for whatever you want to click").one("click",function() {
       $dcac1kids.prop("disabled",false);
    }
}

I've cached the results of the selector on the assumption that you're not adding more elements to the div between the page load and the click. And I've attached the click handler with .one() since you haven't specified a requirement to re-disable the elements so presumably the event only needs to be handled once. Of course you can change the .one() to .click() if appropriate.

6
  • I want to have it on page load ? can it be jquery ?
    – Emax
    Commented Dec 8, 2011 at 22:27
  • The last block of code in my answer does it on page load (well, on document ready which is a better choice for this) and it does it with jQuery - isn't that what you want?
    – nnnnnn
    Commented Dec 8, 2011 at 23:53
  • It is not working ? stackoverflow.com/questions/8468761/…
    – Emax
    Commented Dec 12, 2011 at 12:43
  • Interesting... your answer does work while using children() won't work: stackoverflow.com/a/8423836/114029 Why? I do see in Chrome Dev Tools that it correctly selects the children elements but they won't be disabled at all. Commented Sep 9, 2014 at 3:59
  • @LenielMacaferi - I don't know without seeing your html or perhaps a demo if you create one at jsfiddle.net. But in a general sense .children() only selects immediate children, which is why my answer uses .find() to get all matching descendent elements. You need to select form elements, it doesn't really make sense to disable divs.
    – nnnnnn
    Commented Sep 9, 2014 at 8:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.