Firstly, the identifier of the elements must always be unique. If item identifiers are not unique, you will always get conflicting results. Imagine that in your case two different elements with the same identifier are used.
<form>
a: <input type="number" name="a" id="a"><br>
b: <input type="number" name="b" id="b"><br>
<button onclick="add()">Add</button>
</form>
<script>
function add() {
var a = document.getElementById('a').value;
var b = document.getElementById('b').value;
var sum = parseInt(a) + parseInt(b);
alert(sum);
}
</script>
source
share