Jump to content


checkbox groups (*solved*)


  • Please log in to reply
8 replies to this topic

#1 Guest_huey4657_*

Guest_huey4657_*
  • Guests

Posted 08 August 2006 - 08:44 PM

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.

#2 manmadareddy

manmadareddy
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts

Posted 09 August 2006 - 06:30 AM

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


#3 Guest_huey4657_*

Guest_huey4657_*
  • Guests

Posted 09 August 2006 - 08:27 AM

Sorry mate, your code doesn't work. It does not check any other box but itself.

#4 manmadareddy

manmadareddy
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts

Posted 09 August 2006 - 11:05 AM

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

#5 Guest_huey4657_*

Guest_huey4657_*
  • Guests

Posted 09 August 2006 - 11:07 AM

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.

#6 manmadareddy

manmadareddy
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts

Posted 09 August 2006 - 11:08 AM

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...
<!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>


#7 Guest_huey4657_*

Guest_huey4657_*
  • Guests

Posted 09 August 2006 - 11:13 AM

No need to be sorry, the help is much appreciated  'manmadareddy' thanks.  ;D

#8 Guest_huey4657_*

Guest_huey4657_*
  • Guests

Posted 09 August 2006 - 01:19 PM

I see if the input id's do not contain the obj.id string then indexOf returns '-1', clever.

#9 manmadareddy

manmadareddy
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts

Posted 10 August 2006 - 05:09 AM

just put one if condition like
if(obj.id)
{
  then......
}




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users