0

I'm trying to make a simple addition calculator. You put two values into the boxes and click the "+" sign and the third box has the sum. However, it looks like everything is right to me but when I click the + sign nothing happens

Here is a picture of my code so far

2
  • Check the console - your code will be throwing an error because you mistyped (or misremembered) the getElementById method. And please post code as code, not as an image (let alone a link to an image). Commented Nov 6, 2019 at 21:04
  • 2
    1. There is no getElementsById method, because it is not meant to have two elements with the same id. There is only getElementById, and it returns one element (not an array). 2. Please do not post your code as image. Commented Nov 6, 2019 at 21:07

1 Answer 1

3

First you should change getElementsById() to getElementById() because there is only one id per element.

<input type="text" id="box1"><br/>
<input type="text" id="box2"><br/>
<input type="text" id="+"><br/>
<input type="button" value="+" onclick="calcSum()"><br/>

<script>
function calcSum(){
let box1 = document.getElementById("box1").value;
let box2 = document.getElementById("box2").value;
let sum = Number(box1) + Number(box2);
document.getElementById("+").value = sum;
}
</script>

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.