I have created an input form in html and trying to get the value of this input field using JavaScript.
When I alert it, to check if it works, it returns an empty value. The code is below. What could be the problem?
var num1 = document.getElementById('numb1').value;
var num2 = document.getElementById('numb2').value;
var button = document.getElementsByTagName('button');
var show = document.getElementById('shows');
for (let i = 0; i < button.length; i++) {
if(button[i].id == 'plus'){
button[i].onclick = function (){
var a = num1 + num2;
alert(a);
}
}
}
<div class="container">
<div class="set">
<input type="text" id="numb1" placeholder="enter a number" >
<input type="text" id="numb2" placeholder="enter a number">
<div class="buttons">
<button id="plus">+</button>
<button id="min">-</button>
<button id="mult">*</button>
<button id="div">/</button>
</div>
<div class="show" id="shows"></div>
</div>
</div>