200

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>
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Marquinio
  • 4,601
  • 13
  • 45
  • 68

5 Answers5

456

The following css statement disables click events

pointer-events:none;
bukka
  • 4,973
  • 3
  • 19
  • 21
  • 5
    Really good one, but there are some that today doesn't suport it: http://caniuse.com/pointer-events – thatsIT Jan 23 '14 at 15:48
  • This one is really good for select2 disabling as the built-in feture for that doesn't really work. – user164863 Mar 26 '14 at 09:07
  • 37
    +10, StackOverflow never lets me down. Nevermind about the obsolete IE users, I warn them to upgrade their browser always. – andreszs Sep 23 '14 at 23:28
  • Lol I wish the real world was as simple as "Nevermind about the obsolete IE users". Sadly I still have to support IE10 since public service department are slow at updating *Sigh* So I can't use what would have been perfect: pointer-events :'-( – Gertsen May 18 '16 at 08:09
  • 14
    Unfortunately you can still navigate in between elements with Tab key (and once you navigated to an element, you can edit it as well). – interrupt Mar 25 '17 at 17:46
  • 4
    The standard `disabled` attribute on form fields addresses all forms of input, not just the mouse. This only addresses disabling mouse input. – Roy Tinker May 30 '17 at 19:37
  • 1
    As @RoyTinker said, this does not affect form elements. With whatever logic you're adding the `pointer-events: none` to your div, also have all form elements within that "disabled"-looking div to have the `disabled=true` attribute as well. – Andrew E Dec 12 '18 at 22:01
  • @AndrewE note the `disabled` attribute doesn't have to have a value in HTML 4-5 (i.e. `` is fine). However, if your document is XHTML (or you're using a library that requires a value), the correct value to use is `disabled` (i.e. ``). – Roy Tinker Dec 14 '18 at 01:11
66

Try this!

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

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

Danwilliger
  • 1,221
  • 10
  • 7
17

javascript and 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()" />
SwissCoder
  • 2,514
  • 4
  • 28
  • 40
Nikhil sHETH
  • 510
  • 4
  • 11
  • 4
    'disabled' doesn't work on divs – Mandeep Jain Jan 17 '18 at 10:51
  • Working fine for me in Chrome, Mozilla and IE. just checked. Will you copy paste above example and check it? – Nikhil sHETH Jan 17 '18 at 14:07
  • @MandeepJain, for your convenience I add code to snippet. Run above snippet. – Nikhil sHETH Jan 17 '18 at 14:14
  • Thanks for replying on this old post. But, only form and form elements can be disabled. Your code disables all the input elements but not the div itself. – Mandeep Jain Jan 17 '18 at 15:33
  • @MandeepJain I think you want to disabled div click event right? check above code. Now whole div is disabled as per your requirement. Just update me its working for you or not. – Nikhil sHETH Jan 18 '18 at 07:11
  • 1
    Yes. But your original answer said pure javascript no jquery which was my requirement. Its ok. Thanks for the efforts. I have found another solution :) – Mandeep Jain Jan 18 '18 at 07:42
10

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()

Jai
  • 74,255
  • 12
  • 74
  • 103
2

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/

Spencer Lockhart
  • 1,164
  • 7
  • 7