473,503 Members | 1,360 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Disabling everything in div section

I have a webpage with a form. Depending on user selections at the top of
the page, the page will disable sections of the form. My plan is to put
each section between a <div></div>. Each section contains some collection
of form elements.

So if the user does not select some criteria, the related section of the
form gets disabled. In a typical Windows type of application these elements
get disabled and greyed out. What's the best way to do this in a webpage?
Can Javascript determine what form elements are in a div section and
disable these elements through a loop for example? I know I can disable
each element if I know its it but is there a way to find out all the IDs of
elements for some div section?

John Dalberg
Mar 2 '06 #1
3 33097
You're going to want to scan the child nodes of the DIV sections. For
instance, if the HTML looks like this:

<div id="div_1">
<input type="text" name="firstname">
<input type="text" name="lastname">
<p>ya ya ya</p>
</div>

in the DOM tree the DIV might look like:

DIV
|_input node (<input>)
|_input node (<input>)
|_text node (<p>)

I don't have my JS reference on me, but I'm pretty sure you'll want to
do something like

var div_to_disable = document.getElementById( 'div_1' );
var children = div_to_disable.childNodes;
for (var i = 0; i < children.length; i++)
{
// do stuff here ... such as :
if( children[i].type == 'text' )
children[i].disabled = true;
};

Please be aware, this is really off the cuff and not terribly accurate;
I did more of this a while back. If you're interested and continue to
do this kind of stuff, I'd really recommend getting O'Reilly's
javascript reference. You can do a lot to your DHTML using the DOM
methods. Hopefully this will get you in the right direction.

Mar 2 '06 #2
bobzimuta wrote:
You're going to want to scan the child nodes of the DIV sections. For
instance, if the HTML looks like this:

<div id="div_1">
<input type="text" name="firstname">
<input type="text" name="lastname">
<p>ya ya ya</p>
</div>

in the DOM tree the DIV might look like:

DIV
|_input node (<input>)
|_input node (<input>)
|_text node (<p>)

I don't have my JS reference on me, but I'm pretty sure you'll want to
do something like

var div_to_disable = document.getElementById( 'div_1' );
var children = div_to_disable.childNodes;


That will only grab the direct child nodes of 'div_1', form controls
nested deeper than that (say inside label elements, tables or divs) will
be descendants of the collection of nodes referenced by 'children'.

An alternative is to use a recursive function to travel down the tree
starting at 'div_to_disable', or to use getElementsByTagName('*') to
grab all the elements inside the div (older browsers may not support the
use of '*'). Loop through them and mdofiy the disabled attribute of
relevant elements.
/*
* id - string
* ID of element containing controls to disable/enable
*
* disabled - boolean
* true will disable, false will enable
*/

function setDisabled(id, disabled)
{
if ( !document.getElementById
|| !document.getElementsByTagName) return;

var nodesToDisable = {button :'', input :'', optgroup :'',
option :'', select :'', textarea :''};

var node, nodes;
var div = document.getElementById(id);
if (!div) return;

nodes = div.getElementsByTagName('*');
if (!nodes) return;

var i = nodes.length;
while (i--){
node = nodes[i];
if ( node.nodeName
&& node.nodeName.toLowerCase() in nodesToDisable ){
node.disabled = disabled;
}
}
}
Call with:

<input type="button" value="Disable div_1"
onclick="setDisabled('div_1', true)">

<input type="button" value="Enable div_1"
onclick="setDisabled('div_1', false)">
You could also put the names of nodes to disable in a string and test
with a regular expression, or put them in an array and loop through it.
[...]
--
Rob
Mar 3 '06 #3
RobG wrote:
bobzimuta wrote:
var div_to_disable = document.getElementById( 'div_1' );
var children = div_to_disable.childNodes;
That will only grab the direct child nodes of 'div_1',


True.
form controls nested deeper than that (say inside label elements, tables
or divs) will be descendants of the collection of nodes referenced by
'children'.


No, they will be descendants of the HTML elements represented by items
(Node object references) of the HTMLCollection object referenced by
'children'.
PointedEars
Mar 3 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
2077
by: dukeleto | last post by:
I know this is an annoying thing on some sites. I have set some images in an online gallery to have their own java po up window that is set to be the same size as the image. I would like to...
5
5031
by: Lyn | last post by:
Hi, I hope someone can help. I have a main form which mostly fills the Access window. In the bottom half of this form I have a tab control to display various types of data related to the main...
4
2243
by: louise raisbeck | last post by:
Hi there, I have put some web controls, textboxes and drop down lists, in a Panel, so that under a certain condition i can disable all fields for input. I read in Help that disabling a Panel...
3
1602
by: PB | last post by:
What is the rationalle for disabling JavaScript. AFAIK, the primary reason is for "security purposes" - but what specific kind of threats does the protect against? AND - is the disabling of...
1
2908
by: Matt Van Mater | last post by:
I'm looking to get a little more performance out of my database, and saw in the docs a section about disabling autocommit by using the BEGIN and COMMIT keywords. My problem is this: I enforce...
9
2154
by: Paul Keegstra | last post by:
Hi, I am currently working on an asp.net 2.0 web site that is a replacement of a classic asp web site. The current web site uses a Commerce Server 2002 database for storing user information. ...
2
2430
by: dm1608 | last post by:
I have to issues: 1) Does anyone know of a programmatic way I can disable all textboxes, combo, listboxes, and buttons on a form? 2) What is the best practice for disabling multiple controls...
1
2183
by: Robert | last post by:
I borrowed one of the forms from the MS Access Solutions database and altered it to fit my needs. The form was the 'EditProducts' form where you select a category from a combo which then populates...
11
12092
by: EagleRed | last post by:
I am writing an ASP.NET 2.0 application that uses master pages. I have some pages that must not be cached on the client. In ASP.NET 1.1 I achieved this using metatags: <meta...
0
7203
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7089
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7339
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6995
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5581
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3157
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
389
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.