I have a requirement to create a sample HTML document for an Online calculator and I have used below code to meet the requirement:
<html>
<head>
<title>Rechner</title>
<link href="style.css" rel="stylesheet">
</head>
<body>
<div id="container">
<center>
<form>
<input type="number" placeholder="Zahl 1" name="num1" id="num1" class="num" autofocus required><br>
<input type="number" placeholder="Zahl 2" name="num2" id="num2" class="num" required><br>
<output placeholder="Ergebnis" class="text" id="Ergebnis"><br>
<button class="btn" onclick="berechnen()">Rechne!</button>
</form>
</center>
</div>
<script>
function berechnen() {
var zahl1 = document.getElementsById("num1").value;
var zahl2 = document.getElementsById("num2").value;
var ergebnis = Number(zahl1) + Number(zahl2);
document.getElementById("Ergebnis").value = ergebnis;
}
</script>
</body>
</html>
But, whenever I try to calculate any numbers it either displays nothing
or NaN
Can any one help me out, what I am doing wrong here?
Please dont get confused by the german names.