Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Feb 2007
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy Checkbox Groups in Fieldset wont toggle!?

    I've been using this little bit of code to control 4 groups of check boxes on a form;

    Code:
    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;

    Code:
    <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!!

  • #2
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by zplayer View Post
    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.
    Code:
    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;
    }

  • #3
    New Coder
    Join Date
    Feb 2007
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you very much, it worked a treat!



  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •