Why does my function only work once per page load?

I am writing a script that performs calculations with text inputs in a form. For some reason, my calculations are only done once per page load. I have to refresh the page so that the function starts up again when I click the "calculate" button.

Here is the HTML for my buttons:

<input type="button" value="Calculate" onclick="calculate(high, low, common);" />
<input type="reset" />
<div id="result"></div> 

Here is the calculation function (variables are defined elsewhere):

var calculate = function (high, low, common) {
   if (high.length < 1 || low.length < 1 || common.length <1) {
       document.getElementById('result').innerHTML="Please enter a number for all fields.";
   } else {
       if ((high - common) > (common - low)) {
           document.getElementById('result').innerHTML="Result 1.";
        } else if ((common - low) > (high - common)) {
        document.getElementById('result').innerHTML="Result 2.";
        } else if ((common - low) === (high - common)) {
        document.getElementById('result').innerHTML="Result 3.";
        }
      }
   }; 

Am I doing something that prevents a function from starting more than once per page?

+5
source share
2 answers

The problem with your code is that you simply set the high, low, total 1st time the page loads. These values ​​remain the same all the time.

onclick.

- jsfiddle

<!DOCTYPE html>
<html>
<head>
    <title>Calculator</title>
    <meta charset = "UTF-8" />
</head>
<body>

<h1>Calculator</h1>

<!--Form forvalue inputs-->

<form>
    <label for="highRi">Highest:</label><input type ="text" id="highRi" />
    <label for="lowRi">Lowest:</label><input type="text" id="lowRi" />
    <label for="comm">Common Point:</label><input type ="text" id="comm" />

<!--Clicking the button should run the calculate() function-->

    <input type="button" value="Calculate" onclick="calculate(document.getElementById('highRi').value, document.getElementById('lowRi').value, document.getElementById('comm').value);" />
    <input type="reset" />

<!--Div will populate with result after calculation is performed-->

    <div id="result"></div> 
</form>

<script type="text/javascript">


var calculate = function (high, low, common) {
   if (high.length < 1 || low.length < 1 || common.length <1) {
       document.getElementById('result').innerHTML="Please enter a number for all fields.";
   } else {
       if ((high - common) > (common - low)) {
           document.getElementById('result').innerHTML="Result 1.";
        } else if ((common - low) > (high - common)) {
        document.getElementById('result').innerHTML="Result 2.";
        } else if ((common - low) === (high - common)) {
        document.getElementById('result').innerHTML="Result 3.";
    }
}

}; 

</script>

</body>
</html>

.

<!DOCTYPE html>
<html>
<head>
<title>Calculator</title>
<meta charset = "UTF-8" />
</head>
<body>

<h1>Calculator</h1>

<!--Form forvalue inputs-->

<form>
    <label for="highRi">Highest:</label><input type ="text" id="highRi" />
    <label for="lowRi">Lowest:</label><input type="text" id="lowRi" />
    <label for="comm">Common Point:</label><input type ="text" id="comm" />

<!--Clicking the button should run the calculate() function-->

    <input type="button" value="Calculate" onclick="calculate();" />
    <input type="reset" />

<!--Div will populate with result after calculation is performed-->

    <div id="result"></div> 
</form>

<script type="text/javascript">


var calculate = function () {
    var high = document.getElementById('highRi').value;
    var low = document.getElementById('lowRi').value
    var common = document.getElementById('comm').value
   if (high.length < 1 || low.length < 1 || common.length <1) {
       document.getElementById('result').innerHTML="Please enter a number for all fields.";
   } else {
       if ((high - common) > (common - low)) {
           document.getElementById('result').innerHTML="Result 1.";
        } else if ((common - low) > (high - common)) {
        document.getElementById('result').innerHTML="Result 2.";
        } else if ((common - low) === (high - common)) {
        document.getElementById('result').innerHTML="Result 3.";
    }
}

}; 

</script>

Jsfiddle , , .

+1

, , , , ,

<input type="button" id="button" value="Calculate" />

var button = document.getElementById("button");
var high = document.getElementById("high").value;
var low = document.getElementById("low").value;
var common = document.getElementById("common").value;
button.addEventListener(onclick, calculate(high, low, common));
0

All Articles