5

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 ?

7 Answers 7

5

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Maybe a little bigger than expected (14KB), but it really works. It evens prevent the user from user the keyboard to interact with the elements. Lets see if anyone else comes with the one-line solution before accepting this answer.
3

Something along the lines of :

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

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

1 Comment

$("#mydiv input").attr('disabled', 'true'); Is cleaner and probably faster.
1

What about a div over the disabled div?

<style>

    div.disableall { filter:alpha(opacity=25);-moz-opacity:.25;opacity:.25; background-color: black; height: 100%; width: 100%; position: absolute; top: 0px; left: 0px; z-index: 500; /* just preventing */ }

</style>

And add to the div that you want to disable:

<script type="text/javascript">
    var disable = document.createElement("div");
    disable.className = 'disableall';
    document.getElementById('mydiv').appendChild(disable);
</script>  

This, i guess, should work.

ps: You should fix the z-index according to your needs

Comments

1

This is the DOM traversal method, but it's not that complicated:

$('#mydiv').find('input').attr('disabled','true');
$('#mydiv *').unbind();

3 Comments

I think you want $('#mydiv *').unbind();
Cool! Always finding out new stuff with jQuery.
Does the unbind method prevent the user from selecting elements with the keyboard (navigating with the Tab key)?
0

What quickly springs to my mind is to use either a translucent div hovering above the old content (with such CSS options as "position: relative" and "top: -123px") or adding READONLY into your forms and buttons.

But, which ever way you choose, do remember that these changes are only clientside. Both JavaScript, HTML and CSS are very easy to alter at runtime. So, be prepared for such things also at the server side of things.

Comments

0

You can create a div to cover this dynamically - in prototype it'd be something like this:

var el = new Element("div", {style: "position: absolute; z-index: 2"});
Element.insert(document.body, el);
Element.clonePosition(el, $("mydiv"));

And then style as you'd like to show it disabled.

But if you have a good idea of what elements are in the area, it seems like disabling is the better option - this prevents keyboard access to the elements - tabbing to them and entering data or using the return key to activate them.

Comments

0

The jQuery BlockUI Plugin lets you simulate synchronous behavior when using AJAX, without locking the browser[1]. When activated, it will prevent user activity with the page (or part of the page) until it is deactivated. BlockUI adds elements to the DOM to give it both the appearance and behavior of blocking user interaction.

http://malsup.com/jquery/block/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.