-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
133 lines (106 loc) · 4.18 KB
/
script.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
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
// Get the necessary elements from the HTML code
const sendMoneyForm = document.querySelector('#send-money form');
const transactionHistoryTable = document.querySelector('#transaction-history table tbody');
const clearHistoryButton = document.querySelector('#clear-history');
const clearUserButton = document.querySelector('#clear-user');
const addUserForm = document.querySelector('#add-user form');
const userListTable = document.querySelector('#user-list tbody');
// Create an array to store the transaction history data
let transactionHistory = [];
// Create an array to store the user data
let userList = [];
// Function to add a new transaction to the history array and update the HTML table
function addTransactionToHistory(recipient, email, amount) {
// Create a new transaction object with the current date and time
const transaction = {
date: new Date(),
recipient: recipient,
email: email,
amount: amount
};
// Add the transaction to the transaction history array
transactionHistory.push(transaction);
// Create a new row in the HTML table for the transaction
const newRow = document.createElement('tr');
newRow.innerHTML = `
<td>${transaction.date.toLocaleString()}</td>
<td>${transaction.recipient}</td>
<td>${transaction.email}</td>
<td>${transaction.amount}</td>
`;
// Add the new row to the HTML table
transactionHistoryTable.appendChild(newRow);
}
// Event listener for the send money form submission
sendMoneyForm.addEventListener('submit', function(event) {
event.preventDefault(); // Prevent the default form submission behavior
// Get the input values from the form
const recipient = document.querySelector('#recipient').value;
const email = document.querySelector('#email').value;
const amount = document.querySelector('#amount').value;
const sendMoneybtn = document.getElementById("send-money-btn");
sendMoneybtn.addEventListener("click", function() {
// Code to add user goes here
if (!recipient || !email || !amount) {
alert('Please fill out all fields before submitting the form.');
return;
}else{
// Show alert when user is added
alert("Money Sending successfully!");
}
});
// Add the transaction to the transaction history
addTransactionToHistory(recipient, email, amount);
// Reset the form
sendMoneyForm.reset();
});
// Event listener for the clear history button
clearHistoryButton.addEventListener('click', function() {
// Remove all rows from the HTML table
transactionHistoryTable.innerHTML = '';
// Clear the transaction history array
transactionHistory = [];
});
// Function to add a new user to the HTML table
function addUserToTable(username, email, balance) {
const newUserRow = document.createElement('tr');
newUserRow.innerHTML = `
<td>${username}</td>
<td>${email}</td>
<td>${balance}</td>
`;
userListTable.appendChild(newUserRow);
}
// Event listener for the add user form submission
addUserForm.addEventListener('submit', function(event) {
event.preventDefault(); // Prevent the default form submission behavior
// Get the input values from the form
const username = document.querySelector('#username').value;
const email = document.querySelector('#add-user #email').value;
const balance = document.querySelector('#balance').value;
const addUserBtn = document.getElementById("add-user-btn");
addUserBtn.addEventListener("click", function() {
// Code to add user goes here
if (!username || !email || !balance) {
alert('Please fill out all fields before submitting the form.');
return;
}else{
// Show alert when user is added
alert("User added successfully!");
}
});
// Add the user to the user list
addUserToTable(username, email, balance);
// Reset the form
addUserForm.reset();
});
// Event listener for the clear history button
clearUserButton.addEventListener('click', function() {
// Remove all rows from the HTML table
userListTable.innerHTML = '';
// Clear the transaction history array
userList = [];
});
// Populate the user list with some initial data
// addUserToTable('Alice', '[email protected]', 1000);
// addUserToTable('Bob', '[email protected]', 2000);