bikey77 bikey77 - 1 month ago 14
Javascript Question

Javascript checkbox onChange

I have a checkbox in a form and I'd like it to work according to following scenario:

if someone checks it, the value of a textfield (

totalCost
) should be set to
10
. Then, if I go back and uncheck it, a function
calculate()
sets the value of
totalCost
according to other parameters in the form.

So basically, I need the part where, when I check the checkbox I do one thing and when I uncheck it, i do another.

Answer
function calc()
{
  if (document.getElementById('xxx').checked) 
  {
      document.getElementById('totalCost').value = 10;
  } else {
      calculate();
  }
}

HTML

<input type="checkbox" id="xxx" name="xxx" onclick="calc();"/>