PDA

View Full Version : checkbox addition/subtraction



mini_minh
09-07-2005, 06:01 PM
i've got a checkbox what i want it to do when it is checked is to add 10.00 to a text field and when it is unchecked it subtracts 10.00
what i've got so far is:

<script language="JavaScript"><!--
function calculate(what) {
what.answer.value =0;
for (var i=0,answer=0;i<1;i++)
answer += (what.elements['Checkbox' + i].checked)&(what.elements['Checkbox' + i].value-'10.00');
what.answer.value = answer;
}
//--></script>

<form ID="Form2">
<INPUT type="checkbox" ID="Checkbox0" NAME="Checkbox0" VALUE="1"
onclick="calculate(this.form)">
<input type="text" name="answer" ID="Text13">
</form>

if i click on the checkbox it only adds 1 to the text field T_T
any help would be greatly appreciated

jscheuer1
09-07-2005, 06:35 PM
<script type="text/javascript">
<!--
function calculate(what) {
if (Math.abs(what.answer.value)!==Math.abs(what.answer.value)){
alert ('Numbers Only Please!')
return;
}
what.answer.value = (what.elements['Checkbox0'].checked)? parseInt(what.answer.value, 10)-10 : parseInt(what.answer.value, 10)+10;
}
//-->
</script>

<form>
<input type="checkbox" name="Checkbox0" onclick="calculate(this.form)">
<input type="text" name="answer" value="0">
</form>