First, document.getElementById("number1")
will return only HTML Element, if you want to get the value inside the textbox you have to use document.getElementById("number1").value
...
Second, you place the two statements, var number1 = document. ...
and var number2 = document. ...
outside the decisionDemo()
method, so that, it will be executed only once. You should place those statements inside the decisionDemo()
method.
Third, document.getElementById("number1").value
will return string
value. So, if you want to perform arithmetic calculation, you need to parse them to a sort of number, such as Integer or Float.
function decisionDemo(){
var number1 = document.getElementById("number1").value;
var number2 = document.getElementById("number2").value;
number1 = parseFloat(number1);
number2 = parseFloat(number2);
if(confirm("Would you like to multiply or add?")){
alert("You multiplied for the result of "+ number1*number2);
}
else{
alert("You added for the result of "+ (number1+number2));
}
}
You might face NaN in result when user enter invalid numbers.
You can use isNaN() to check whether your input is valid or not