PDA

View Full Version : select all / unselect al checkbox



mtran
05-17-2007, 12:18 AM
How can I write something like this and make it work:


function select_all(field){
if ((field+"_all")[0].checked==false){
for (i=0; i<field.length; i++)
field[i].checked = true;}}...


I want to make a Select/Unselect All checkbox, which is named the same as the rest of the checkboxes except the "_all" part. Ex: all checkboxes name = "country", that Select all box name="country_all"

I want to use only one checkbox instead of 2 buttons/checkboxes - one to Check all and one to Uncheck all.
I have different areas with very long list of checkboxes.
Thanks for help!

mburt
05-17-2007, 12:40 AM
Use:

var fields = field.getElementsByTagName("input");
and, if you wanted to narrow it:

if (fields[i].type == "checkbox") //code here

mtran
05-17-2007, 01:06 AM
Not quite sure how to apply it.
Say you have:



<input type="checkbox" name="country_eu_all">Select all/Deselect all
<input type="checkbox" name="country_eu" value="1">England
.....
<input type="checkbox" name="country_eu" value="20">Germany

<input type="checkbox" name="country_asia_all">Select all/Deselect all
<input type="checkbox" name="country_asia" value="1">Japan.
....
<input type="checkbox" name="country_asia" value="20">Singapore


I have about 7 of these long lists, and want to create a function that I can apply to all. Thanks for help

rajug
05-17-2007, 03:34 AM
See this function:


function CheckAll(){
if(document.frmls.chkAll.checked == true){
for(var j = 0; j < document.frmls.length; j++){
if(document.frmls.elements[j].name == "ids[]"){
document.frmls.elements[j].checked = true;
}
}
}
else if(document.frmls.chkAll.checked == false){
for(var j = 0; j < document.frmls.length; j++){
if(document.frmls.elements[j].name == "ids[]"){
document.frmls.elements[j].checked = false;
}
}
}
}

Where my HTML form is :


<form name="frmls" action="">
<table width="100%" border="0" cellspacing="1" cellpadding="2" style="border:1px #CCCCCC solid;">
<tr>
<td width="4%" align="center" valign="middle" bgcolor="#CCCCCC"><input name="chkAll" type="checkbox" id="chkAll" value="1" onClick="CheckAll();"></td>
<td width="2%" height="22" align="center" valign="middle" bgcolor="#CCCCCC">&nbsp;</td>
<td width="19%" align="left" valign="middle" bgcolor="#CCCCCC"><strong>Company</strong></td>
<td width="18%" align="left" valign="middle" bgcolor="#CCCCCC"><strong>Project</strong></td>
<td width="20%" align="left" valign="middle" bgcolor="#CCCCCC"><strong>Start Date </strong></td>
<td width="23%" align="left" valign="middle" bgcolor="#CCCCCC"><strong>Estimated Due By</strong></td>
<td width="14%" align="left" valign="middle" bgcolor="#CCCCCC">&nbsp;</td>
</tr>
<?php
if($objDb->NumRows($result) >= 1){
while($rows = $objDb->FetchRows($result)){
$bgcolor = ($bgcolor == "#E4E4E4") ? "#FFFFFF" : "#E4E4E4";
if($rows['hwbAntwerpen'] == 0){
$bgcolor = "#B38280";
}
?>
<tr bgcolor="<?php echo $bgcolor;?>">
<td align="center" valign="middle"><input name="ids[]" type="checkbox" id="ids[]" value="<?php echo $rows['lead_id'];?>" onClick="this.form.chkAll.checked=false;"></td>
<td align="center" valign="middle"><img src="modules/leadmgmt/images/pencil.gif" width="9" height="9"></td>
<td align="left" valign="top">
<?php
echo $objLib->GetParticularName("companies", "company_name", "company_id", $rows['project_company']);
?>
</td>
<td align="left" valign="top"><?php echo $rows['project_name'];?></td>
<td align="left" valign="top"><?php echo $rows['leadfromdate'];?></td>
<td align="left" valign="top"><?php echo $rows['leadEstimatedDate'];?></td>
<td align="center" valign="top">
<?php
if($rows['hwbAntwerpen'] == 1){
?>
<a href="index.php?m=leadmgmt&p=view_details&project_id=<?php echo $rows['project_id'];?>&lead_id=<?php echo $rows['lead_id'];?>">View Details</a>
<?php
}else{
?>
<a href="index.php?m=leadmgmt&p=lead_add&project_id=<?php echo $rows['project_id'];?>&lead_id=<?php echo $rows['lead_id'];?>">Edit Nepal</a>
<?php
}
?></td>
</tr>
<?php
}
?>
<tr>
<td align="left" colspan="7"><input type="button" name="btnDelete" id="btnDelete" value="Delete Selected" onClick="CheckIfChecked();" />&nbsp;
<input name="action" type="hidden" id="action" value="">
&nbsp;&nbsp;<span style="width:25px;height:25px;background-color:#B38280;">&nbsp;&nbsp;&nbsp;</span> Remained to fill up from nepal. Please click on <span style="color:#0000FF;text-decoration:underline;">Edit Nepal</span> link on the right.</td>
</tr>
<?php
}else{
?>
<tr>
<td align="center" colspan="7">There are no lead project(s) found.</td>
</tr>
<?php
}
?>
</table>
</form>


This may help you.