PHP/JS check all, uncheck all button for checkboxes

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
ant_sutton
Forum Commoner
Posts: 32
Joined: Thu May 05, 2005 5:27 am

PHP/JS check all, uncheck all button for checkboxes

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

document.forms['formName'].elements['elementName'] should give you an array of the checkboxes.
ant_sutton
Forum Commoner
Posts: 32
Joined: Thu May 05, 2005 5:27 am

re

Post 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
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Why not search the forum?
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

Post Reply