HTML File
<body>
<form name = 'add'>
<div name = 'add'>
<input type="number" id='num1'>
<input type="number" id='num2'>
<button name = 'sum' onclick="add()"></button>
<input type="number" id="sum">
</div>
</form>
<script src='additionkindergarden.js'></script>
JS file
var num1,num2,sum;
function add(){
num1 = document.getElementById('num1').value;
num2 = document.getElementById('num2').value;
sum = num1 + num2;
return document.getElementById('sum').value = sum;
}
Just started to code without the use of Udemy. I'm stuck here for about 5 hrs. I know its simple but I can't get my JS to connect with my HTML. I put in the number in num1 and num2 places in the HTML when I press the button the screen does a quick refresh, the numbers in num1 and num 2 disappear and the last input box is left blank. Please help with my first solo lvl. 1 coding project.
add()but also submit the form, which here will reload the page. Remove the<form>tags, you don't need them. Next, you needsum = +num1 + +num2;(otherwise 1 + 2 => 12)valueof a numeric input is aNumberso no conversion needed.valueis "A Number representing the value of the number entered into the input".