-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
159 lines (135 loc) · 5.26 KB
/
index.html
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript Tutorial</title>
<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
crossorigin="anonymous"
/>
<script
defer
src="https://use.fontawesome.com/releases/v5.7.2/js/all.js"
integrity="sha384-0pzryjIRos8mFBWMzSSZApWtPl/5++eIfzYmTgBBmXYdhvxPc+XcFEk+zJwDgWbP"
crossorigin="anonymous"
></script>
</head>
<body>
<nav class="navbar navbar-dark bg-dark">
<span class="navbar-brand mb-0 h1">Xpers</span>
<span class="navbar-brand mb-0 h1" id="headingTotal"></span>
</nav>
<div class="container-fluid bg-light" style="min-height: 100vh">
<div class="container">
<div class="jumbotron mt-4">
<div class="input-group mb-3">
<input
type="text"
class="form-control"
id="inputAmount"
placeholder="Amount spent"
aria-label="Amount spent"
aria-describedby="spent in rupee"
/>
<div class="input-group-append">
<span class="input-group-text" id="spent in rupee">₹</span>
</div>
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="spent at">@</span>
</div>
<input
type="text"
class="form-control"
id="inputDesc"
placeholder="spent On"
aria-label="spent On"
aria-describedby="spent at"
/>
<div class="input-group-append">
<button class="btn btn-outline-primary" type="button" id="btnAddExpense">Add</button>
</div>
</div>
</div>
<div>
<ul class="list-group" id="expenseTable">
</ul>
</div>
<script>
//get the heading element
const headingEl= document.querySelector("#headingTotal");
//get the desc element
const descEl= document.querySelector('#inputDesc')
//ref to input amount
const inputElement = document.querySelector("#inputAmount");
//ref to table
const expenseTableEl= document.querySelector('#expenseTable');
//initialize the value of expense at 0
let totalExpense = 0;
//set the heading element to totalExpense
headingEl.textContent=totalExpense;
//onButtonClick add the value of inputAmount to totalExpense
//total expense
const allExpense=[];
function addExepensetoTotal() {
const expenseItem={};
//read value from inputAmount
const textAmount = inputElement.value;
//read value from decription text
const textDesc= descEl.value;
//convert it into number
const inputExpense = parseInt(textAmount, 10);
//add it to totalExpense
totalExpense = totalExpense + inputExpense;
//fill the expense object
expenseItem.desc= textDesc;
expenseItem.amount=inputExpense;
expenseItem.moment= new Date();
//set the heading element to totalExpense
const someText=`Total: ${totalExpense}`;
headingEl.textContent=someText;
allExpense.push(expenseItem);
const allExpenseHTML= allExpense.map(expense=> createListItem(expense));
const allExpensejoined =allExpenseHTML.join("");
expenseTableEl.innerHTML = allExpensejoined;
}
// get the button element
const element = document.querySelector("#btnAddExpense");
// console.log(element);
//listen to the click event
element.addEventListener("click", addExepensetoTotal, false);
//controller functions
function getDateString(moment){
return moment.toLocaleDateString('en-US',{
year: 'numeric',
month: 'long',
day: 'numeric',
});
}
//view layer
function createListItem({desc,amount,moment}) {
return `
<li class="list-group-item d-flex justify-content-between">
<div class="d-flex flex-column">
${desc}
<small class="text-muted">${getDateString(moment)}</small>
</div>
<div>
<span class="px-5">
${amount}
</span>
<button type="button" class="btn btn-outline-danger btn-sm">
<i class="fas fa-trash-alt"></i>
</button>
</div>
</li>
`
}
</script>
</body>
</html>