Page 1 of 1

PHP/JS check all, uncheck all button for checkboxes

Posted: Fri Nov 24, 2006 8:39 am
by ant_sutton
Hi guys.

I would really appreciate some help with this as I cant for the life of me get it to work.

I have a series of checboxes created in a php script. The name of the checboxes is weeks[]. This creates an array when the different weeks are seelected so I can input all the weeks that are checked into a DB.

However, just to make things easier I want to create a button that checks and unchecks all the checboxes to make things easier. I have tried adding some javascript but I don't think it likes it when I use an array as the checkbox name. Does anyone know how I can do this while maintaining the array?

thanks alot

Posted: Fri Nov 24, 2006 8:46 am
by feyd
document.forms['formName'].elements['elementName'] should give you an array of the checkboxes.

re

Posted: Fri Nov 24, 2006 11:38 am
by ant_sutton
Thanks for your reply feyd. Not really sure how to use that code though:)

here is what I'm trying to use so far

Code: Select all

<SCRIPT LANGUAGE="JavaScript">
<!-- 

<!-- Begin
function CheckAll(chk)
{
for (i = 0; i < chk.length; i++)
chk[i].checked = true ;
}

function UnCheckAll(chk)
{
for (i = 0; i < chk.length; i++)
chk[i].checked = false ;
}
// End -->
</script>



<form name="myform" action="makerequest2.php" method="post">
Weeks:<input type="checkbox" name="weeks[]" value="1">week1<br>
<input type="checkbox" name="weeks[]" value="2">week2<br>
<input type="checkbox" name="weeks[]" value="3">week3<br>
<input type="checkbox" name="weeks[]" value="4">week4<br>
<input type="checkbox" name="weeks[]" value="5">week5<br>

<input type="button" name="Check_All" value="Check All"
onClick="CheckAll(document.myform.check_list)">
<input type="button" name="Un_CheckAll" value="Uncheck All"
onClick="UnCheckAll(document.myform.check_list)"> <br />
thanks

Anthony

Posted: Fri Nov 24, 2006 11:51 am
by jayshields
Why not search the forum?

Posted: Fri Nov 24, 2006 12:15 pm
by matthijs