Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable HTML element in the client side (javascript)

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 ?

like image 909
Guido Avatar asked Feb 02 '09 20:02

Guido


People also ask

Can you disable a div in JavaScript?

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.

How do I disable an element?

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.

How do I disable a span tag?

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 } .


2 Answers

The jQuery plugin BlockUI is a great way to achieve this.

like image 176
Vincent Robert Avatar answered Sep 28 '22 00:09

Vincent Robert


Something along the lines of :

$("#mydiv").find("input").attr('disabled','disabled');

Which basically finds all input elements in the myDiv element, and disables them.

like image 25
Andreas Grech Avatar answered Sep 28 '22 00:09

Andreas Grech