0

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>

2 Answers 2

1

This because you have kept following lines outside the callback function:

var num1 = document.getElementById('numb1').value;
var num2 = document.getElementById('numb2').value;

So, num1 and num2 are initialized only once i.e. at page load-time. At this time both (num1 and num2) having empty value. Hence it not being initialized every time and showing and empty value.

Note:

  • Consider to parse input text into numeric values using parseInt() or parseFloat()
  • You should keep your JavaScript code in <script> tag.

Following is corrected code snippet:

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 num1 = document.getElementById('numb1').value;
			var num2 = document.getElementById('numb2').value;
				var a = parseFloat(num1 )+ parseFloat(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>

Sign up to request clarification or add additional context in comments.

Comments

0

You need to get the value of the inputs when the button is clicked, not when the page loads.

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 num1 = parseFloat(document.getElementById('numb1').value);
        var num2 = parseFloat(document.getElementById('numb2').value);
        var a = num1 + num2;
        alert(a);
    }   
} 
}

The above code will get the value of the inputs when the button is clicked. I also presumed you would want the values converting to float, if this is not the case then remove the parseFloat function to make it:

var num1 = document.getElementById('numb1').value;
var num2 = document.getElementById('numb2').value;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.