0

I have a script with a simple formula:

Input 1 * Input 2 * Input 3 / 25 = result

How can I manipulate the value 25 if a checbox is checked?

For example, if a checkbox is checked, the input 1,2,3 should divide with 35 at the end instead of 25 and display the result divided by 35.

Sample fiddle.

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
g5wx
  • 700
  • 1
  • 10
  • 30

5 Answers5

2
function calculate() {
    var myBox1 = document.getElementById('box1').value; 
    var myBox2 = document.getElementById('box2').value;
    var myBox3 = document.getElementById('box3').value;
    var result = document.getElementById('result');
    var divide = (document.getElementById('changeValue').checked ? 35 : 25);
    var myResult = myBox1 * myBox2 * myBox3 / divide;
    result.value = myResult.toFixed(2) + ' eggs';
}
blablabla
  • 1,468
  • 15
  • 17
  • p.s. Is there a way to append a text string inside the input after the result value. For example, 1,984 eggs. And also to limit the result to two decimals? – g5wx Mar 25 '15 at 15:17
  • Added the word 'eggs' to the answer. Check this SO answer for correct rounding of numbers. http://stackoverflow.com/questions/11832914/round-to-at-most-2-decimal-places-in-javascript/25075575#25075575 – blablabla Mar 25 '15 at 15:35
  • Added the rounding from the SO answer. – blablabla Mar 25 '15 at 15:37
  • For two decimals use `myResult.toFixed(2)`. Not sure where `Math.round10` even comes from. – Sebastian Simon Mar 25 '15 at 15:45
  • You are right. toFixed is a better / simpler option here. The Math.round10 method is from the SO question provided. – blablabla Mar 25 '15 at 15:48
0

Change

var myResult = myBox1 * myBox2 * myBox3 / 25;

for

var divisor = ((document.getElementById('changeValue').checked)?35:25);
var myResult = myBox1 * myBox2 * myBox3 / divisor ;
Dominique Fortin
  • 2,212
  • 15
  • 20
0

Basic check to see if a checkbox is checked.

var isChecked = document.getElementById("changeValue").checked;  //get boolean for checked
var num = isChecked ? 35 : 25;  //ternary operation

References: Conditional_Operator

epascarello
  • 204,599
  • 20
  • 195
  • 236
0

You can do it by adding:

var value = (document.getElementById('changeValue').checked)?35:25;

Like that:

function calculate() {
    var myBox1 = document.getElementById('box1').value; 
    var myBox2 = document.getElementById('box2').value;
    var myBox3 = document.getElementById('box3').value;
    var result = document.getElementById('result'); 
    var value = (document.getElementById('changeValue').checked)?35:25;
    var myResult = myBox1 * myBox2 * myBox3 / value;
    result.value = myResult;    
}
rm4
  • 711
  • 4
  • 15
0
function calculate() {
    var myBox1 = document.getElementById('box1').value;   
    var myBox2 = document.getElementById('box2').value;
    var myBox3 = document.getElementById('box3').value;
    var result = document.getElementById('result');   
    var myResult = myBox1 * myBox2 * myBox3 / 25;
    result.value = myResult;   

    //set initial state
     $('#changeValue').change(function() {
         if($(this).is(":checked")) {
          var myDivide = myBox1 * myBox2 * myBox3 / 35;
          return result.value = myDivide; 
         }     
     });
}
Michal Kucaj
  • 681
  • 5
  • 15