I'm trying to multiply a number from a textbox with other values through a function with parameters. For some reason I keep getting "NaN" as a result.
Here's a watered down version of the code:
function calculate(a, b, c, d) {
var num1 = a * b;
var num2 = num1 * a;
document.write(num1);
document.write(num2);
}
<input type="number" id="apple" min="0" value="0">
<input type="button" onclick="calculate('apple','1.99','2','5')" value="Add to Order">
The document.write lines are just there for test purposes. I just don't know why num1 and num2 give back "NaN". Also, I've tried doing a.value and b.value to no avail.
var num1 = document.getElementById(a).value*bwhich will multiply the value of your number with the pricenum1 = NaNthennum2will beNaNtoo, because you multiplenum1which isNaNwithapplewhich is string and thennum2will returnNaNtoo'apple' * '1.99'o_O What did you expect this to do?