-
Notifications
You must be signed in to change notification settings - Fork 0
/
expense.js
39 lines (29 loc) · 1.38 KB
/
expense.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
document.addEventListener('DOMContentLoaded', () => {
const form = document.getElementById('expense-form');
const descriptionInput = document.getElementById('description');
const amountInput = document.getElementById('amount');
const totalExpenseElem = document.getElementById('expense');
const expenseList = document.getElementById('expense-list');
let totalExpense = 0;
form.addEventListener('submit', (e) => {
e.preventDefault();
const description = descriptionInput.value;
const amount = parseFloat(amountInput.value);
if (description && !isNaN(amount)) {
const listItem = document.createElement('li');
const descriptionSpan = document.createElement('span');
descriptionSpan.className = 'item-description';
descriptionSpan.textContent = description;
const amountSpan = document.createElement('span');
amountSpan.className = 'item-amount';
amountSpan.textContent = `$${amount.toFixed(2)}`;
listItem.appendChild(descriptionSpan);
listItem.appendChild(amountSpan);
expenseList.appendChild(listItem);
totalExpense += amount;
totalExpenseElem.textContent = `$${totalExpense.toFixed(2)}`;
descriptionInput.value = '';
amountInput.value = '';
}
});
});