person314314314
person314314314

Reputation: 63

Getting from html form to javascript variable

I'm new to programming. I'm trying to build a program so when a user submits 2 integers into the form, it outputs the sum.

Here's what I got so far

<html>
<h1> The Adder </h1>
<form>
  Enter your first number<input type="text" id='a'> <br>
  Enter your second number<input type="text"id='b' <br>
  <input type= "button" onclick='add()' value="Submit"/>
</form>
<script>
function add(){
    var avalue= document.getElementById('a');
    var bvalue= document.getElementById('b');

    alert(avalue+bvalue); 
}
</script>
</html>

Upvotes: 2

Views: 51

Answers (1)

Samurai
Samurai

Reputation: 3729

You need to use value to get the value of the input:

var avalue= document.getElementById('a').value;

And the value will be a string, so you'll need parseInt to convert it to integer:

var avalue= parseInt(document.getElementById('a').value);

Upvotes: 1

Related Questions