This question already has an answer here:
1)I want to add the values of two input boxes and show it in the third input. It shows an error
2)Error: Uncaught TypeError: Cannot read property 'value' of null at sum (js.php:21) at HTMLButtonElement.onclick (js.php:12)
<!DOCTYPE html>
<html>
<head>
<title>JavaScript</title>
<script type="text/javascript">
function sum() {
var val1 = document.getElementById('val1').value;
var val2 = document.getElementById('val2').value;
var sum = val1 + val1;
document.getElementById('total').value = sum;
}
</script>
</head>
<body>
<input type="text" name="val1"><br><br>
<input type="text" name="val2"><br><br>
<input disabled name="total" value=""><br><br>
<button type="button" onclick="sum()" name="btn">Add</button>
</body>
</html>
I expected the sum of two inputs but it shows nothing.
</div>