PDA

View Full Version : problem determining whether checkbox is on or off



tempest4000
Jul 2nd, 2009, 02:22 PM
All I want to do is determine whether the selected checkbox is on or off. At the moment, when the checkbox is ticked or unticked the javascript returns false. Any help much appreciated.



<form name="update_active" method="POST">
<input type="checkbox" name="active" onClick="updateActive(1, 'product')" />1
<input type="checkbox" name="active" onClick="updateActive(2, 'product')" />2
<input type="checkbox" name="active" onClick="updateActive(3, 'product')" />3
<input type="checkbox" name="active" onClick="updateActive(4, 'product')" />4
</form>

[CODE]//is checkbox on or off?
function updateActive(id, type)
{
//is checkbox on or off?
for (var i=0; i < document.update_active.active.length; i++)
{

if (document.update_active.active[i].value == 'y'){
var active = 'y';
} else {
var active = 'n';
}

}
alert(active);
}CODE]

vwphillips
Jul 2nd, 2009, 03:54 PM
function updateActive(id, type)
{
//is checkbox on or off?
for (var i=0; i < document.update_active.active.length; i++)
{

if (document.update_active.active[i].checked{
var active = 'y';
} else {
var active = 'n';
}

}
alert(active);
}

tempest4000
Jul 2nd, 2009, 04:34 PM
Ive changed this and it still dowesn't work. Its returns 'n' therefore returns false when checked or unchecked. Ive also tried:
if (document.update_active.active[i].checked == true){
var active = 'y';
} else {
var active = 'n';
}
this also does nto work

tempest4000
Jul 2nd, 2009, 04:37 PM
hhhhmmm, Ive just realised that the last checkbox in the list works as it should (ie. outputs Y when selected and N when unselected), but all the checkboxes above this return N regardless.

vwphillips
Jul 2nd, 2009, 04:53 PM
oops


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<script type="text/javascript">
/*<![CDATA[*/
function updateActive(nu, type)
{

if (document.update_active.active[nu].checked){
var active = 'y';
} else {
var active = 'n';
}

alert(active);
}
/*]]>*/
</script></head>

<body>
<form name="update_active" method="POST">
<input type="checkbox" name="active" onClick="updateActive(0, 'product')" />1
<input type="checkbox" name="active" onClick="updateActive(1, 'product')" />2
<input type="checkbox" name="active" onClick="updateActive(2, 'product')" />3
<input type="checkbox" name="active" onClick="updateActive(3, 'product')" />4
</form>
</body>

</html>

tempest4000
Jul 2nd, 2009, 05:09 PM
Yes that works, thanks you.