PDA

View Full Version : help me to resolve this checkbox problem



FLC
11-25-2004, 04:00 AM
somewhere in the body tag I have this coding

<input type="hidden" name="hiddenName_#ContractList.ContractID#" value="#Description#" />
<input type="checkbox" name="contracts" value="#ContractList.ContractID#" />

the variable #ContractList.ContractID# comes from a query in coldfusion

and I have a header checkbox that trigger checkall() function

and the code of the function is

function checkAll()
{
var contractBoxes = document.ContractForm.contracts;

for (var i = 0; i < contractBoxes.length; ++i)
{
if (contractBoxes[i].checked == true)
{
contractBoxes[i].checked = false;
}
else
{
contractBoxes[i].checked = true;
}
}
}


the document.ContractForm.contracts; refer to a input form and contracts is a checkbox name

my problem is,

if there is only 1 row of record, the contractBoxes.length will become undefined and will not go into the for loop

if I force it by doing this

function checkAll()
{
var contractBoxes = document.ContractForm.contracts;

if (contractBoxes.length == null)
{
contractBoxes.length = 1;
}

for (var i = 0; i < contractBoxes.length; ++i)
{

if (contractBoxes[i].checked == true)
{
contractBoxes[i].checked = false;
}
else
{
contractBoxes[i].checked = true;
}
}
}

it goes into the for loop but it does not toggle my checkbox

just imagine this scenario with yahoo mailbox where there is a checkbox at the left side, you click the header checkbox, all the checkboxes will be checked,

hemebond
11-25-2004, 05:11 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>47976</title>
</head>
<body>
<form name="ContractForm">
<input type="hidden" name="hiddenName_#ContractList.ContractID#" value="#Description#">
<input type="checkbox" name="contracts" value="#ContractList.ContractID#">
<input type="button" value="check all" onclick="checkAll()">
</form>

<script type="text/javascript">
function checkAll()
{
var contractBoxes = document.ContractForm.contracts;
if (contractBoxes.length == undefined)
{
contractBoxes = new Array(contractBoxes);
}

for (var i = 0; i < contractBoxes.length; ++i)
{
contractBoxes[i].checked = (contractBoxes[i].checked) ? false : true;
}
}
</script>
</body>
</html>

FLC
11-25-2004, 05:46 AM
I'm really impressed with your knowledge in javascript

really thank you very much for solving this problem...

it is working very well