how to select all the checkbox

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
jaylin
Forum Commoner
Posts: 68
Joined: Fri Nov 18, 2005 4:44 am

how to select all the checkbox

Post 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
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post 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:
Post Reply