i was trying to sum 2 input of type text(I've already a function that allows only floats so the format isn't a problem) and i don't know why it keeps returning a NaN and how to fix it. Can anyone help me? html:
<form>
<input type="text" id="num1"/>
<p2>+</p2>
<input type="text" id="num2"/>
<input type="submit" value="Calc" onclick="calc()"/>
<p2 id="res"></p2>
</form>
javascript:
function calc() {
var res;
//...
document.getElementById("res").innerHTML=res;
}
You can compute the sum as follows:
function calc() {
var a = parseFloat(document.getElementById("num1").value);
var b = parseFloat(document.getElementById("num2").value);
var res = a + b
document.getElementById("res").innerHTML = res.toString();
}