I am trying to continuously add to a js variable every time a user enters a value into a box.
So far if they enter \'21\' the alert will say \'your balance is £12\' but
The function which makes the change is attached to a submit button.
When the user clicks the button:
var firstAmount = 0;
in itYou should:
Using an onclick attribute, you need to return false from the event handler function:
onclick="depositedFunds(); return false;"
Modern code would separate concerns and not tie things so tightly to a specific means of triggering the form submission.
var firstAmount = 0;
function depositedFunds(e) {
e.preventDefault();
var ad = document.getElementById("amountDropped");
firstAmount = +firstAmount + +ad.value;
alert("Your balance is £" + firstAmount);
};
document.querySelector('form').addEventListener('submit', depositedFunds);
<form method="get">
<input type="number" id="amountDropped">
<input type="submit" value="Deposit amount">
</form>