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
1 Answer
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>

getElementByIdmethod. And please post code as code, not as an image (let alone a link to an image).getElementsByIdmethod, because it is not meant to have two elements with the same id. There is onlygetElementById, and it returns one element (not an array). 2. Please do not post your code as image.