Currently making an expense tracker. One of my functions adds an expense to a div titled history. This is the function:
function addExpense()
{
clothingID={'id':Date.now(),'clothingType':clothingType.value, 'brand': brand.value, 'amnt':amnt.value, };
clothingArray.push(clothingID);
count1++;
showTotalBalance();
showDifference();
expenseHistory();
clothingType.value='';
amnt.value='';
brand.value = '';
setItemInStorage();
}
Once the user inputs an expense, it should look something like this (please excuse bad design):
Originally, this was done through the following code:
<form id = "clothingExpenses" class = 'display'>
<div style = 'text-align:center;' class="row">
<div style = 'text-align:center;' class="six columns">
<label style = ' padding-bottom: 1pt; text-align:center;' for="exampleEmailInput">Clothing type</label>
<input id = 'type' astyle = "width: 50%;" type="email" placeholder="Enter the clothing type" id="exampleEmailInput">
</div>
</div>
<div>
<label style = ' padding-bottom: 1pt;' for="exampleEmailInput">Amount</label>
<input id="amnt" style = "width: 24%;" type="number" placeholder="Enter number" id="exampleEmailInput">
</div>
<div>
<label style = 'padding-bottom: 1pt;' for="exampleEmailInput">Clothing brand</label>
<input id="brand" style = "width: 24%;" type="text" placeholder="Enter the clothing brand" id="exampleEmailInput">
</div>
<button type = 'button' class="button-primary" onclick="addExpense()">Add expense</button>
<button type = 'button' class="button-primary" onclick="closeExpenseForm()">Close form</button>
</form>
The button with the onclick action of addExpense() would execute this function fine. However, I needed to alter the code in order to have an action for the form, which would send user input to a Google Sheet. I therefore had to move the onclick action up in my form element.
Here is the updated form:
<form method="POST" action = "https://script.google.com/macros/s/AKfycbyoRaf291Z8ud57I70QwyHv7alefLhul3WLKpFCc3K0FjlEIWHzsoN8ZmC2jKSEg/exec" onclick = "addExpense()" id = "clothingExpenses" class = 'display'>
<div class="row">
<div class="six columns">
<label>Clothing type</label>
<input name = "Type" id = 'type' style = "width: 50%;" type="text" placeholder="Enter the clothing type" id="exampleEmailInput">
</div>
</div>
<div>
<label>Amount</label>
<input name = "Amount" id="amnt" style = "width: 24%;" type="number" placeholder="Enter number" id="exampleEmailInput">
</div>
<div>
<label style = 'padding-bottom: 1pt;' for="exampleEmailInput">Clothing brand</label>
<input name = "Brand" id="brand" style = "width: 24%;" type="text" placeholder="Enter the clothing brand" id="exampleEmailInput">
</div>
<button type = 'submit' class="button-primary">Add expense</button>
<button type = 'submit' class="button-primary" onclick="closeExpenseForm()">Close form</button>
</form>
After moving the onclick action further up, the addExpense() function now runs when simply clicking on an input element as such:
And provides the following output:
Any ideas on why this is occurring and how I can fix this?





