2

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>
1
  • 3
    You should use: var avalue= document.getElementById('a').value; AND var bvalue= document.getElementById('b').value; Commented May 21, 2015 at 21:12

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

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.