I am trying to make an program which will add two number and i am a beginner i got my stored in a variable but cannot convert it to number
var num1 = Number.parseInt(num0)
i tried using parse int but i still dont get the correct input
heres my full code
<html>
<body>
<input type="text" id="text" placeholder="Number 1"></input>
<input type="text" id="text2" placeholder="Number 2"></input>
<button type="submit" id="submit" onclick="put()">Click Me</button>
<p id="myp"></p>
<script>
function put() {
var num0 = document.getElementById("text")
var num1 = Number.parseInt(num0)
var num4 = document.getElementById("text2")
var num2 = Number.parseInt(num4)
var sub = document.getElementById("submit")
var res = num1 num2
document.getElementById("myp").innerHTML = num1.value num2.value
}
</script>
</body>
</html>
if i try num1.value its undefined and if only try num1 its considered as text
CodePudding user response:
function put() {
var num0 = document.getElementById("text")
var num1 = Number(num0.value)
var num4 = document.getElementById("text2")
var num2 = Number(num4.value)
var sub = document.getElementById("submit")
var res = num1 num2
document.getElementById("myp").innerHTML = num1 num2
}
CodePudding user response:
function put() {
var num0 = document.getElementById("text").value
var num1 = Number.parseInt(num0)
var num4 = document.getElementById("text2").value
var num2 = Number.parseInt(num4)
var res = num1 num2
document.getElementById("myp").innerHTML = res
}