I am looking for the simplest way to disable a whole HTML element (typically a <div>
).
With "disable" I mean a way to prevent the user to click on any of the elements contained into the element. For instance, if you have a form into the disabled element, the form elements won't catch the focus. I've tried with a method that traverses the DOM and disables each element it finds. It works but it seems too complex and I'm looking for a simpler method, something like $("#mydiv").disable()
I would like to prevent the user to fire other events like onmouseover, etc. and even avoid the user to select and modify elements using the keyboard. Maybe some kind of semitransparent overlay covering the whole element to be disabled could do the trick.
Is that possible with jQuery or do you know an easy way to implement it with plain JS ?
Can you disable a div JavaScript? Using JavaScript In plain JavaScript, you can get all the children of the div and disable them within a loop. The idea is to use the getElementsByTagName() method, which returns a collection of elements with the given tag name.
An element can be disabled in HTML by setting disable property to true and enabled again by setting disabled=false. By using jQuery, you can grab the element you want to enable or disable and change this property by using the prop() or attr() function, depending upon the version of jQuery you are using.
This answer could be extended to work globally by simply using css: span[disabled] { pointer-events: none; } . This way you don't need to worry about resetting pointer-events to auto. Or you could use a class like . clicks-disabled { pointer-events: none } .
The jQuery plugin BlockUI is a great way to achieve this.
Something along the lines of :
$("#mydiv").find("input").attr('disabled','disabled');
Which basically finds all input
elements in the myDiv element, and disables them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With