PDA

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



leetee4000
07-02-2009, 01:53 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>


//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].checked){
var active = 'y';
} else {
var active = 'n';
}

}
alert(active);
}

Nile
07-03-2009, 03:40 AM
This should do it:

<script type="text/javascript">
var active = new Array();
var updateActive = function(key, me){
if(me.checked == true){
active[key] = true;
} else {
active[key] = false;
}
alert(active[key]);
};
</script>
<input type="checkbox" onClick="updateActive(1, this)" />
<input type="checkbox" onClick="updateActive(2, this)" />


active[key] is the result of it being checked or not.

Good Luck!