-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform.html
More file actions
153 lines (128 loc) · 4.01 KB
/
Copy pathform.html
File metadata and controls
153 lines (128 loc) · 4.01 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<style>
h1,
h2 {
text-align: center;
}
#formContainer {
text-align: center;
margin-bottom: 20px;
}
label {
display: block;
margin-top: 10px;
}
input,
textarea {
width: 300px;
padding: 8px;
margin-top: 5px;
border: 1px solid #ccc;
border-radius: 4px;
}
button {
margin: 10px;
padding: 8px 16px;
background-color: #4CAF50;
color: #fff;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
table {
border-collapse: collapse;
margin-bottom: 20px;
width: 100%;
}
th,
td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #4CAF50;
color: #fff;
}
</style>
</head>
<body>
<h2>User Data Management</h2>
<div id="formContainer">
<label for="nameInput">
Name:
</label>
<input type="text" id="nameInput" placeholder="Enter Name...">
<label for="emailInput">
Email ID:
</label>
<input type="email" id="emailInput" placeholder="Enter Email...">
<br>
<button onclick="addData()">Add</button>
</div>
<table id="outputTable">
<tr>
<th>Name</th>
<th>Email</th>
<th>Action</th>
</tr>
</table>
<script>
function addData() {
// Get input values
let name = document.getElementById("nameInput").value;
let email = document.getElementById("emailInput").value;
// Check if name or email is empty
if (!name || !email) {
alert("Both name and email are required!");
return;
}
// Get the table and insert a new row at the end
let table = document.getElementById("outputTable");
let newRow = table.insertRow(table.rows.length);
// Insert data into cells of the new row
newRow.insertCell(0).innerHTML = name;
newRow.insertCell(1).innerHTML = email;
newRow.insertCell(2).innerHTML =
'<button onclick="editData(this)">Edit</button>' +
'<button onclick="deleteData(this)">Delete</button>';
// Clear input fields
clearInputs();
}
function editData(button) {
// Get the parent row of the clicked button
let row = button.parentNode.parentNode;
// Get the cells within the row
let nameCell = row.cells[0];
let emailCell = row.cells[1];
// Prompt the user to enter updated values
let nameInput = prompt("Enter the updated name:", nameCell.innerHTML);
let emailInput = prompt("Enter the updated email:", emailCell.innerHTML);
// Check if inputs are not empty before updating
if (nameInput && emailInput) {
// Update the cell contents with the new values
nameCell.innerHTML = nameInput;
emailCell.innerHTML = emailInput;
} else {
alert("Both fields are required!");
}
}
function deleteData(button) {
// Get the parent row of the clicked button
let row = button.parentNode.parentNode;
// Remove the row from the table
row.parentNode.removeChild(row);
}
function clearInputs() {
// Clear input fields
document.getElementById("nameInput").value = "";
document.getElementById("emailInput").value = "";
}
</script>
</body>
</html>