...

View Full Version : How do I make an ENABLE function out of this working DISABLE function ??



mOrloff
08-20-2010, 03:23 AM
I found this script, and it works great:


<script type="text/javascript">
function disable(element)
{
var input = document.getElementById(element).getElementsByTagName("input");
for(var i = 0; i < input.length; i++)
{
input.setAttribute("disabled","true");
}
}
</script>


I tried to make the inverse by simply reversing the setAttribute() like so:


<script type="text/javascript">
function enable(element)
{
var input = document.getElementById(element).getElementsByTagName("input");
for(var i = 0; i < input.length; i++)
{
input[i].setAttribute("disabled","false");
}
}
</script>


But that didn't do it.
Can someone show me why, and how to fix it?

Here's the sample form which I'm trying to test it on:


<form>
<input type="radio" name="test" onclick="disable('D1')" /> disable<br/>
<input type="radio" name="test" onclick="enable('D1')" /> enable<br/>
<fieldset id="D1">
<input class="" type="text" value="test value1" /><input class="" type="text" value="test value2" /><br/>
<input class="" type="text" value="test value3" /><input class="" type="text" value="test value4" /><br/>
<input class="" type="text" value="test value5" /><input class="" type="text" value="test value6" /><br/>
</fieldset>
</form>



The ultimate goal which I'm working toward now (step by step =) is to have a form more like:


<form>
<input type="radio" name="test" onclick="disable('D1')" />
<fieldset id="D1">
<input class="" type="text" value="test value1" /><input class="" type="text" value="test value2" />
</fieldset>
<input type="radio" name="test" onclick="disable('D2')" />
<fieldset id="D2">
<input class="" type="text" value="test value3" /><input class="" type="text" value="test value4" />
</fieldset>
<input type="radio" name="test" onclick="disable('D3')" />
<fieldset id="D3">
<input class="" type="text" value="test value5" /><input class="" type="text" value="test value6" />
</fieldset>
</form>

And have the fieldsets enable [I]and disable according the selection of the radio buttons.

Also, the fieldsets (and their ID's) will be dynamically generated via PHP


Thanks-a-bunch,
~ Mo

mrhoo
08-20-2010, 03:59 AM
// There are two ways to do it, but setting the disabled attribute to the string 'false' won't work.
// Disabled is one of those boolean attributes that is defined as true in html if the attribute exists at all, even without a value.


function enable(element){
var input= document.getElementById(element).getElementsByTagName("input");
for(var i= 0; i < input.length; i++){
input[i].removeAttribute("disabled");
}
}



function enable(element){
var input= document.getElementById(element).getElementsByTagName("input");
for(var i= 0; i < input.length; i++){
input[i].disabled=false;
}
}

mOrloff
08-20-2010, 04:28 AM
Ahah!
Thanks.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum