how to disable DIV element and everything inside

JavascriptJqueryHtmlCss

Javascript Problem Overview


I need to disable a DIV and all it's content using Javascript. I can swear that doing a simple

<div disabled="true"> 

was working for me before, but for some reason it no longer works. I don't understand why.

In IE10: the text "Click Me" is not greyed out and click handler still works.

I actually need this working for IE10. Below is my code.

<html>
    <script>
         function disableTest(){

            document.getElementById("test").disabled = true;
            var nodes = document.getElementById("test").getElementsByTagName('*');
            for(var i = 0; i < nodes.length; i++){
                nodes[i].disabled = true;
            }

         }


     </script>

<body onload="disableTest();">
   <div id="test">
       <div onclick="alert('hello');">
           Click Me
       </div>
   </div>

</body>
</html>

Javascript Solutions


Solution 1 - Javascript

The following css statement disables click events

pointer-events:none;

Solution 2 - Javascript

Try this!

$("#test *").attr("disabled", "disabled").off('click');

I don't see you using jquery above, but you have it listed as a tag.

Solution 3 - Javascript

pure javascript no jQuery

function sah() {
		$("#div2").attr("disabled", "disabled").off('click');
		var x1=$("#div2").hasClass("disabledDiv");
		
		(x1==true)?$("#div2").removeClass("disabledDiv"):$("#div2").addClass("disabledDiv");
  sah1(document.getElementById("div1"));

}

    function sah1(el) {
        try {
            el.disabled = el.disabled ? false : true;
        } catch (E) {}
        if (el.childNodes && el.childNodes.length > 0) {
            for (var x = 0; x < el.childNodes.length; x++) {
                sah1(el.childNodes[x]);
            }
        }
    }

#div2{
  padding:5px 10px;
  background-color:#777;
  width:150px;
  margin-bottom:20px;
}
.disabledDiv {
    pointer-events: none;
    opacity: 0.4;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<div id="div1">
        <div id="div2" onclick="alert('Hello')">Click me</div>
        <input type="text" value="SAH Computer" />
        <br />
        <input type="button" value="SAH Computer" />
        <br />
        <input type="radio" name="sex" value="Male" />Male
        <Br />
        <input type="radio" name="sex" value="Female" />Female
        <Br />
    </div>
    <Br />
    <Br />
    <input type="button" value="Click" onclick="sah()" />

Solution 4 - Javascript

I think inline scripts are hard to stop instead you can try with this:

<div id="test">
    <div>Click Me</div>
</div>

and script:

$(function () {
    $('#test').children().click(function(){
      alert('hello');
    });
    $('#test').children().off('click');
});

###CHEKOUT FIDDLE AND SEE IT HELPS

Read More about .off()

Solution 5 - Javascript

You can't use "disable" to disable a click event. I don't know how or if it worked in IE6-9, but it didn't work on Chrome, and it shouldn't work on IE10 like that.

You can disable the onclick event, too, by attaching an event that cancels:

;(function () {
    function cancel () { return false; };
    document.getElementById("test").disabled = true;
    var nodes = document.getElementById("test").getElementsByTagName('*');
    console.log(nodes);
    for (var i = 0; i < nodes.length; i++) {
        nodes[i].setAttribute('disabled', true);
        nodes[i].onclick = cancel;
    }
}());

Furthermore, setting "disabled" on a node directly doesn't necessarily add the attribute- using setAttribute does.

http://jsfiddle.net/2fPZu/

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
QuestionMarquinioView Question on Stackoverflow
Solution 1 - JavascriptbukkaView Answer on Stackoverflow
Solution 2 - JavascriptDanwilligerView Answer on Stackoverflow
Solution 3 - JavascriptNikhil sHETHView Answer on Stackoverflow
Solution 4 - JavascriptJaiView Answer on Stackoverflow
Solution 5 - JavascriptSpencer LockhartView Answer on Stackoverflow