Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In HTML can I disable user interaction with an entire DOM sub-tree?

Tags:

html

In HTML can I disable user interaction with an entire DOM sub-tree via CSS?

like image 858
Ben Aston Avatar asked Oct 21 '15 16:10

Ben Aston


People also ask

How do I disable interactions in HTML?

Use CSS pointer-events property on an element to disable user interaction on it and its descendants. You could also use the user-select property to further restrict the user interaction of text selection.

How do I disable DOM element?

The DOM Option disabled Property is used to set or return whether the value of an option would be disabled or not. The disabled attribute for the element in HTML is used to specify that the option value is disabled. A disabled option is un-clickable and unusable.


1 Answers

Use CSS pointer-events property on an element to disable user interaction on it and its descendants.

div.disabled { pointer-events: none; } 

You could also use the user-select property to further restrict the user interaction of text selection.

div.disabled { user-select: none; } 

Note that the user-select property may need vendor prefixes.


However, these CSS properties will not disable keyboard interactions which could happen by way of tabbing into the descendants.

As per this reference -- https://html.spec.whatwg.org/multipage/interaction.html#inert-subtrees; there is a proposal of an inert property but it is not yet implemented by browsers.

So, as of now you will resort to Javascript to disable keyboard interactions. Just wire up the keydown event with capture and set the returnValue to false. Do not forget to allow Tab key to allow for an escape out, otherwise the focus could get trapped.

var noInteracts = document.getElementsByClassName('disabled'); [].map.call(noInteracts, function(elem) {     elem.addEventListener("keydown", function(e) {         if (e.keyCode != 9) {       // allow tab key to escape out             e.returnValue = false;             return false;         }     }, true); }); 

You could also hide the highlight-outline on focus of inputs by this CSS:

div.disabled *:focus { outline: 0; } 

Below is a demo with all of the above techniques combined.

Demo fiddle: http://jsfiddle.net/abhitalks/hpowhh5c/5/

Snippet:

var noInteracts = document.getElementsByClassName('disabled');  [].map.call(noInteracts, function(elem) {      elem.addEventListener("keydown", function(e) {          if (e.keyCode != 9) {              e.returnValue = false;              return false;          }      }, true);  });
div.disabled {       pointer-events: none;     -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none;     user-select: none;  }  div.disabled *:focus { outline: 0; }
<p>This is normal</p>  <form>      <label>Input 1: <input id="i1" /> </label>      <button>Submit</button>  </form>  <hr />  <div class="disabled">      <p>User interaction is disbled here</p>      <form>          <label>Input 2: <input id="i2" /> </label>          <button>Submit</button>      </form>      <p>Lorem ipsum</p>  </div>
like image 180
Abhitalks Avatar answered Oct 31 '22 00:10

Abhitalks