As of right now, this is a little test piece of Javascript in which you input some values into a form, and then calculate some values based on them. Values are not read directly from the form, but rather from a set of global variables which are updated using the load params button.
What's bugging (literally) me is that in my form, the input attribute type="number"
seems to be being ignored. When I add a submit button to the end of the form (a button that I don't really want), the form works as expected - it blocks me from typing in anything other than numeric characters. However, when I remove this submit button, it doesn't seem to be doing this input validation.
Any idea why?
var params = {
a: 0,
b: 0,
c: 0
}
function parse_inputs() {
document.forms["input_parameters"].submit();
params["a"] = parseFloat(document.getElementById("input_parameters").a_in.value);
params["b"] = parseFloat(document.getElementById("input_parameters").b_in.value);
params["c"] = parseFloat(document.getElementById("input_parameters").c_in.value);
}
function write_params() {
var text = ""
for (var param in params) {
text += params[param] + "<br>"
}
document.getElementById("demo").innerHTML = text;
}
function calc_sum() {
var sum = 0
for (var param in params) {
sum += params[param]
}
document.getElementById("demo").innerHTML = sum;
}
<h2>CLT Calculator</h2>
<form id="input_parameters" autocomplete="off" novalidate="false">
<fieldset>
<legend>Input parameters</legend><br> a: <input type="number" step="0.1" name="a_in" placeholder="0" required><br> b: <input type="number" step="0.1" name="b_in" placeholder="0" required><br> c: <input type="number" step="0.1" name="c_in" placeholder="0"
required><br><br>
</fieldset>
</form>
<br>
<button onclick="parse_inputs()">Load params</button><br>
<button onclick="write_params()">Stored params</button><br>
<button onclick="calc_sum()">Calculate sum</button><br>
<p id="demo"></p>