0

Here is a simple calculation that is run when a checkbox is checked:

function calculate() {
   var guests = document.getElementById("guests").value;
   var drinkRate = 2;
   var drinkCost = drinkRate * guests;

   //output
   document.getElementById("drinkPrice").value = '$' + 
   parseFloat(drinkCost).toFixed(2);
};
Number of guests: 
<input type="number" name="guests" id="guests" value="4"><br>
Drinks<input type="checkbox" name="drinks" id="A" onchange="calculate()"><output id="drinkPrice"></output><br>

How can the calculation be cleared (or hidden) when the check box is unchecked?

Thanks

1
  • You can check the state of a checkbox through its checked property : document.getElementById('A').checked Commented Sep 20, 2018 at 14:36

4 Answers 4

4

You can use the checked property of checkbox to identify the checkbox is checked or unchecked. The demo with your Code as:

function calculate() {

 if (document.getElementById("A").checked) {
  var guests = document.getElementById("guests").value;
  var drinkRate = 2;
  var drinkCost = drinkRate * guests;

  //output
  document.getElementById("drinkPrice").value = '$' +
   parseFloat(drinkCost).toFixed(2);
 } else {
  document.getElementById("drinkPrice").value = "";
 }

};
Number of guests: 
<input type="number" name="guests" id="guests" value="4">
<br>
Drinks: 
<input type="checkbox" name="drinks" id="A" onchange="calculate()">

<output id="drinkPrice"></output><br>

Sign up to request clarification or add additional context in comments.

1 Comment

This answer make more sense because you should not be doing calculation at all if checkbox is unchecked. @Prashant - Good to format code before posting.
1

I would highly suggest you do not use the onchange event, use the onclick instead, for compatibility reasons best explained by @T.J. Crowder on this question

Simply change the onchange="calculate(this)" inside the checkbox tag to:

<input type='checkbox' onclick='calculate(this);'>

Comments

0

Check the checkbox current state and reset the value if the state is false.

function calculate() {
   var guests = document.getElementById("guests").value;
   var drinkRate = 2;
   var drinkCost = drinkRate * guests;

   //output
   document.getElementById("drinkPrice").value = '$' + 
   parseFloat(drinkCost).toFixed(2);

   if(!document.getElementById('A').checked) {
        document.getElementById("drinkPrice").value = ""
   }
};
Number of guests: 
<input type="number" name="guests" id="guests" value="4"><br>
Drinks<input type="checkbox" name="drinks" id="A" onchange="calculate()"><output id="drinkPrice"></output><br>

Comments

0

You have to check if the checkbox check value is true. Try this.

function calculate() {
 if (!document.getElementById("A").checked) {
  document.getElementById("drinkPrice").value = "";
 } else {
  var guests = document.getElementById("guests").value;
  var drinkRate = 2;
  var drinkCost = drinkRate * guests;
  document.getElementById("drinkPrice").value = '$' + parseFloat(drinkCost).toFixed(2);
 }

};
     
 Number of guests: 
<input type="number" name="guests" id="guests" value="4"><br>
Drinks<input type="checkbox" name="drinks" id="A" onchange="calculate()"><output id="drinkPrice"></output><br>

1 Comment

You could greatly improve your answer by explaining your reasoning behind your solution, and how you actually did it. Also you could use a ternary operation to simplify the assignment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.