Networking | Hardware | Software | Multimedia | System | Unix&Linux | MBA

Home>>Hardware>>help me to resolve this checkbox problem

help me to resolve this checkbox problem

FLC
11-25-2004, 03: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, 04: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, 04: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


 

TOP

For more info

setting a minimum size
Auto Preview of Image 
Refresh with a query s
JS Validation obsolete
Javascript Pop up func
Slide show transitions
I'm a newbie...please!
how to write coding in
Advanced Debug Info 
.js file doesn't work 

News Archive

Adding Graphic To Text
re-direct form after s
pop up window controls
Clock on homepage - He
passing a param from a
Password Procection wi
document.title in same
focus onload 
Counting Wrong from Ri
Help needed on how to 

Related stories:

A little help with javascript IF/THEN
Is VBScript "invisible"?
does anyone use Venkman?
How can I use "enter" to submit a form in a frame using javascript?
So confused
View Images in a Directory Script...?
working with a java applet

Copyright@2004-2005 www.zzcoke.com All Right Reserved

advanced web statistics