i have a checkbox that toggled with a button
but i also need to add the its value to an equation, ONLY IF its checked.
what i got right now is this, but it just add regardless of whether the checkbox is checked or not
<html>
<head>
<script language=javascript>
function validate(chk){
if (chk.checked == 1)
chk.checked = 0;
else
chk.checked = 1;
}
</script>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
</head>
<body>
<form name="myform">
<input type="checkbox" name="ghj" id="ghj" value="100000">testing sheep<br>
<input type="button" class="button-primary widget-control-save" name="Check_All" value="Check All" onmouseout="compute(this.form)" onClick="validate(ghj)">
<input type="text" id="rslt" name="rslt">
</form>
<script type="text/javascript">
<!-- hide this script from old browsers
function compute(form)
{
var test = parseInt(document.getElementById('ghj').value, 10) || 0;
f = (test + 10);
document.getElementById('rslt').value = f;
}
// done hiding from old browsers -->
</script>
</body>
</html>
if…else statements may work but if i have a longer form that would mean hundreds of possibilities, and i’d like a more efficient way..
If you want to compute only the checkbox which are checked. Then do something like this
}
See sample code jsfiddle.net/5u5TH