Jump to content

Archived

This topic is now archived and is closed to further replies.

Guest huey4657

checkbox groups (*solved*)

Recommended Posts

Guest huey4657
Hi,
Does anyone out there know how to check certain boxes within a group by calling ID instead of name?

For example - all the checkboxes are named "name = Course[]" but to make them individual they have an id tag "ID = a, ID = b, etc...

So I wish to either have a button or checkbox that controls some of the group. - "a, b, f, and y"

Thks for any help or links.

Share this post


Link to post
Share on other sites
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<SCRIPT LANGUAGE="JavaScript">
<!--
function checkAll(obj)
{
var elements=document.getElementsByTagName("input");
var checkflag=obj.checked;
for(var i=0;i<elements.length;i++)
{
if(elements[i].type=='checkbox')
{
if(elements[i].id.indexOf(obj.id)!='-1')
{
if(checkflag)elements[i].checked=true;
else elements[i].checked=false;
}
}
}
}
//-->
</SCRIPT>
</HEAD>

<BODY>
<FORM METHOD=POST ACTION="">
<TABLE cellpadding=0 cellspacing=0 width='50%' border=1 >
<TR>
<TD><INPUT TYPE="checkbox" NAME="check[]" id='group1' onclick="checkAll(this);">Main group1</TD>
</TR>
<TR>
<TD>
<INPUT TYPE="checkbox" NAME="check[]" id='group1_1'>group11
<INPUT TYPE="checkbox" NAME="check[]" id='group1_2'>group12
<INPUT TYPE="checkbox" NAME="check[]" id='group1_3'>group13
<INPUT TYPE="checkbox" NAME="check[]" id='group1_4'>group14
</TD>
</TR>
<TR>
<TD><INPUT TYPE="checkbox" NAME="check[]" id='group2' onclick="checkAll(this);">Main group2 </TD>
</TR>
<TR>
<TD>
<INPUT TYPE="checkbox" NAME="check[]" id='group2_1'>group21
<INPUT TYPE="checkbox" NAME="check[]" id='group2_2'>group22
<INPUT TYPE="checkbox" NAME="check[]" id='group2_3'>group23
<INPUT TYPE="checkbox" NAME="check[]" id='group2_4'>group24
</TD>
</TR>

</TABLE>
</FORM>
</BODY>
</HTML>

Share this post


Link to post
Share on other sites
Guest huey4657
Sorry mate, your code doesn't work. It does not check any other box but itself.

Share this post


Link to post
Share on other sites
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<SCRIPT LANGUAGE="JavaScript">
<!--
function checkAll(obj)
{
  var elements=document.getElementsByTagName("input");
  var checkflag=obj.checked;
  for(var i=0;i<elements.length;i++)
  {
      if(elements[i].type=='checkbox')
      {
        if(elements[i].id.indexOf(obj.id)!='-1')
        { 
            if(checkflag)elements[i].checked=true;
            else elements[i].checked=false;
        }
      }
  }
}
//-->
</SCRIPT>
</HEAD>

<BODY>
<FORM METHOD=POST ACTION="">
<TABLE cellpadding=0 cellspacing=0 width='50%' border=1 >
<TR>
  <TD><INPUT TYPE="checkbox" NAME="check[]" id='group1' onclick="checkAll(this);">Main group1</TD>
</TR>
<TR>
  <TD>
  <INPUT TYPE="checkbox" NAME="check[]" id='group1_1'>group11
  <INPUT TYPE="checkbox" NAME="check[]" id='group1_2'>group12
  <INPUT TYPE="checkbox" NAME="check[]" id='group1_3'>group13
  <INPUT TYPE="checkbox" NAME="check[]" id='group1_4'>group14
  </TD>
</TR>
<TR>
  <TD><INPUT TYPE="checkbox" NAME="check[]" id='group2' onclick="checkAll(this);">Main group2 </TD>
</TR>
<TR>
  <TD>
  <INPUT TYPE="checkbox" NAME="check[]" id='group2_1'>group21
  <INPUT TYPE="checkbox" NAME="check[]" id='group2_2'>group22
  <INPUT TYPE="checkbox" NAME="check[]" id='group2_3'>group23
  <INPUT TYPE="checkbox" NAME="check[]" id='group2_4'>group24
  </TD>
</TR>

</TABLE>
</FORM>
</BODY>
</HTML>

Share this post


Link to post
Share on other sites
Guest huey4657
Can anyone see where the problem is? I have changed things around but cannot seem to find why it does not work.  ??? Can anyone tell me what this is doing - !='-1'

thks for your help.

Share this post


Link to post
Share on other sites
Hi Iam very sorry.
I didn't saw the preview before posting.
It is omitted all the indexes.
Thats why its not working.
Now U can check...
[code]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<SCRIPT LANGUAGE="JavaScript">
<!--
function checkAll(obj)
{
  var elements=document.getElementsByTagName("input");
  var checkflag=obj.checked;
  for(var i=0;i<elements.length;i++)
  {
      if(elements[i].type=='checkbox')
      {
        if(elements[i].id.indexOf(obj.id)!='-1')
        { 
            if(checkflag)elements[i].checked=true;
            else elements[i].checked=false;
        }
      }
  }
}
//-->
</SCRIPT>
</HEAD>

<BODY>
<FORM METHOD=POST ACTION="">
<TABLE cellpadding=0 cellspacing=0 width='50%' border=1 >
<TR>
  <TD><INPUT TYPE="checkbox" NAME="check[]" id='group1' onclick="checkAll(this);">Main group1</TD>
</TR>
<TR>
  <TD>
  <INPUT TYPE="checkbox" NAME="check[]" id='group1_1'>group11
  <INPUT TYPE="checkbox" NAME="check[]" id='group1_2'>group12
  <INPUT TYPE="checkbox" NAME="check[]" id='group1_3'>group13
  <INPUT TYPE="checkbox" NAME="check[]" id='group1_4'>group14
  </TD>
</TR>
<TR>
  <TD><INPUT TYPE="checkbox" NAME="check[]" id='group2' onclick="checkAll(this);">Main group2 </TD>
</TR>
<TR>
  <TD>
  <INPUT TYPE="checkbox" NAME="check[]" id='group2_1'>group21
  <INPUT TYPE="checkbox" NAME="check[]" id='group2_2'>group22
  <INPUT TYPE="checkbox" NAME="check[]" id='group2_3'>group23
  <INPUT TYPE="checkbox" NAME="check[]" id='group2_4'>group24
  </TD>
</TR>

</TABLE>
</FORM>
</BODY>
</HTML>[/code]

Share this post


Link to post
Share on other sites
Guest huey4657
No need to be sorry, the help is much appreciated  'manmadareddy' thanks.  ;D

Share this post


Link to post
Share on other sites
Guest huey4657
I see if the input id's do not contain the obj.id string then indexOf returns '-1', clever.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.