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