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?
source
share