...

View Full Version : Checkbox Groups in Fieldset wont toggle!?



zplayer
02-13-2007, 05:15 PM
I've been using this little bit of code to control 4 groups of check boxes on a form;



function checkAll(elm,name)
{
for (var i = 0; i < elm.form.elements.length; i++)
{
if (elm.form.elements[i].name.indexOf(name) == 0)
{
elm.form.elements[i].checked = elm.checked;
}
}
}

Each form group is like this;



<fieldset class="radio" id="c">
<label>
<label><input type="checkbox" name="selectall" value="typeAll" checked="checked" onclick="checkAll(this,'a')" />Select all</label>
<label><input name="a1" checked="checked" type="checkbox"/>Roaming</label>
<label><input name="a2" checked="checked" type="checkbox"/>Premium</label>
<label><input name="a3" checked="checked" type="checkbox"/>Video</label>
<label><input name="a4" checked="checked" type="checkbox"/>Local</label>
<label><input name="a5" checked="checked" type="checkbox"/>National</label>
<label><input name="a6" checked="checked" type="checkbox"/>International</label>
</fieldset>


Subsequent group names are b1 to b6, c1 to c6 etc. Only now that I've put it in a fieldset its no longer working and its driving me mad as I cant fix it!!

Arty Effem
02-13-2007, 07:24 PM
Subsequent group names are b1 to b6, c1 to c6 etc. Only now that I've put it in a fieldset its no longer working and its driving me mad as I cant fix it!!Don't get mad - read the console.

Your function is assuming that all the elements through which it cycles, will have a name attribute. A fieldset cannot have that attribute.

function checkAll(elm,name)
{
for (var i = 0; i < elm.form.elements.length; i++)
if (elm.form.elements[i].name && elm.form.elements[i].name.indexOf(name) == 0)
elm.form.elements[i].checked = elm.checked;
}

zplayer
02-14-2007, 10:14 AM
Thank you very much, it worked a treat!

:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum