Page 1 of 1

how to select all the checkbox

Posted: Wed Nov 30, 2005 4:51 am
by jaylin
here is my php code:

Code: Select all

<?php
$id = 0;
		while($result=mysql_fetch_assoc($query))
		{
		
?>
<tr class=text align=center>
	   <td><input type=checkbox name="chk[<?php echo $id?>]" value=<?php echo ($result["ID"])?>></td>
</tr>
<?php		
			$id++;
		}
	}
	}
}
?>
i want to select all the checkbox by javascript. it work fine if i only use checkbox name as chk. but, i have to use these above code for the other purpose.

here is my javascript (that have error)

Code: Select all

function selectall()
	{
		for (i = 0; i <frmweekly.chk.length; i++)
			frmweekly.chk[i].checked=true;
 	}
plz correct my javascript.
regards

Posted: Wed Nov 30, 2005 5:24 am
by n00b Saibot
my One-Size-Fits-All script 8)

Code: Select all

<input type=checkbox onclick="doCheck(this.checked)">
<form name="frm">
<input type=checkbox name="a">
<input type=checkbox name="b">
<input type=checkbox name="c">
<input type=checkbox name="d">
<input type=checkbox name="e">
</form>

<script>
F = document.frm;

function doCheck(state)
{
 for(i=0; i<F.elements.length; i++)
  if(F.elements[i].type == "checkbox")
   F.elements[i].checked = state;
}
</script>
this will check/uncheck all with one checkbox... the first unnamed one. Have Fun :wink: