...

View Full Version : Check All Checkbox



carrera
06-06-2003, 08:11 AM
I found a free script on checking all boxes with one click of select all checkbox. But i don't know how to alter it to fit my script.

j=0
<form name="frmOrders" action="pass.asp" method="post">
while not objrs.eof
<input type="checkbox" name="<%=j%>.stockitem" value="<%=objrs("ID")%>"><br>
objrs.movenext
wend
<br>
<input type=checkbox name="selectall" value="" onClick="selectAll()">
</form>

<script language="Javascript">
function selectAll() {
if (document.frmOrders.countitems.value=="0") {} else
if (document.frmOrders.countitems.value=="1") {
document.frmOrders.stockitem.checked=document.frmOrders.selectall.checked;
} else {
for (var x=0; x<document.frmOrders.countitems.value; x++) {
document.frmOrders.items[x].checked=document.frmOrders.selectall.checked;
}
}
}
</script>

stockitem.checked is not right in my javascript. how can i include the value of <%=j%>.stockitemi in the javascript?

Roelf
06-06-2003, 09:17 AM
dont include that in your script, loop through all form elements and if the elementtype is CHECKBOX, then check it

like:

var elem = document.forms["frmOrders"].elements;
for (i in elem) {
if (i.type == "checkbox" && (i.name.indexof("stockitem") > -1)){
i.checked = true;
}
}

this is not tested code, but it should give you an idea how to handle it

ca_redwards
06-06-2003, 03:55 PM
Originally posted by carrera
I found a free script on checking all boxes with one click of select all checkbox. But i don't know how to alter it to fit my script.

A trick I use relies on declaring the checkboxes as an array of checkbox objects (just give them all the same name, just like radio buttons). Here's a sample...


<form action="">
<input name="scope" type="radio" onclick="setAll(this,true)">All
<input name="scope" type="radio" onclick="setSome(this)">Some
<input checked name="scope" type="radio" onclick="setAll(this,false)">None<br>
<br>
<input name="choice" type="checkbox" onclick="setSome(this)">Bees<br>
<input name="choice" type="checkbox" onclick="setSome(this)">Money<br>
<input name="choice" type="checkbox" onclick="setSome(this)">Psychology<br>
<input name="choice" type="checkbox" onclick="setSome(this)">Web Development<br>
</form>
<script type="text/javascript" language="javascript">
function setSome(chk)
{
var sum=0;
with(chk.form)
{
for(var i=0;i<choice.length;i++)sum+=(choice[i].checked?1:0);
scope[0].checked=(sum==choice.length);
scope[2].checked=(sum==0);
scope[1].checked=(!scope[0].checked && !scope[2].checked);
};
};
function setAll(chk,state)
{
if(chk.checked)with(chk.form)for(var i=0;i<choice.length;i++)choice[i].checked=state;
setSome(chk);
};
</script>

Hopefully, you can adapt this script for your own application.

:ca_redwards.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum