I received input input tags, but no matter what I write in the inputs, it recognizes the value of the string, so I cannot use my conditions.
and the second problem, if I type "ddd" for the first input and "111" for the second input and press the button, it shows NaN on the console. I want to show a warning instead. How can I fix them?
function addFunc() {
var x = document.getElementById("num1").value;
var y = document.getElementById("num2").value;
if (typeof x == 'string' || typeof y == 'string') {
var result = parseInt(x) + parseInt(y);
console.log(result);
} else {
alert("Wrong Entry!");
}
}
<input id="num1">
<input id="num2">
<button type="button" onclick="addFunc()">ADD</button>
<p id="result"></p>
Run code
source
share