-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamic.html
191 lines (175 loc) · 5.65 KB
/
dynamic.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
label {
font-size: 14px;
padding: 4px;
}
input {
padding: 2px;
margin: 4px 0px;
}
.error-msg {
color: red;
font-size: 12px;
}
</style>
</head>
<body>
<div>
<label for="username">Username</label>
<input type="text" name="username" id="username" />
<p id="usernameError" class="error-msg"></p>
</div>
<div>
<label for="password">Password</label>
<input type="password" name="password" id="password" />
<p id="passwordError" class="error-msg"></p>
<button onclick="showPasswordClick()" id="showButton">
show password
</button>
</div>
<div>
<button id="submit">Submit</button>
</div>
<div style="margin-top: 20px">
<table>
<thead>
<th>Username</th>
<th>password</th>
<th>action</th>
</thead>
<tbody id="tableBody">
<tr id="1">
<td id="1Username">Abdulj</td>
<td>pass123</td>
<td>
<button
style="
background-color: white;
color: black;
padding: 2px;
margin: 2px;
"
>
Edit
</button>
<button
style="
background-color: red;
color: white;
padding: 2px;
margin: 2px;
"
onclick="deleteItem('1')"
>
Delete
</button>
</td>
</tr>
</tbody>
</table>
</div>
<script>
const showButton = document.getElementById("showButton");
const passwordInput = document.getElementById("password");
const usernameInput = document.getElementById("username");
const usernameError = document.getElementById("usernameError");
const passwordError = document.getElementById("passwordError");
const tableBody = document.getElementById("tableBody");
let isValid = false;
let id = 2;
let isEdit = false;
let activeId = 2;
function deleteItem(id) {
const item = document.getElementById(id);
item.remove();
}
function editItem(id) {
isEdit = true;
activeId = id;
const itemUser = document.getElementById(id + "username");
const itemPass = document.getElementById(id + "password");
usernameInput.value = itemUser;
passwordInput.value = itemPass;
}
function submitData() {
console.log("form is valid ", tableBody.innerHTML);
tableBody.innerHTML += `
<tr id='${id}'>
<td id='${id}username'>${usernameInput.value}</td>
<td>${passwordInput.value}</td>
<td>
<button style="background-color: white; color: black; padding:2px; margin: 2px;">Edit</button>
<button style="background-color: red; color: white; padding:2px; margin: 2px;" onclick="deleteItem('${id}')">Delete</button>
</td>
</tr>
`;
usernameInput.value = "";
passwordInput.value = "";
id++;
}
function editSubmitData() {
const itemUser = document.getElementById(activeId + "Username");
const itemPass = document.getElementById(activeId).getElementsByTagName("td")[1];
itemUser.innerText = usernameInput.value;
itemPass.innerText = passwordInput.value; isEdit = false;
activeId = 2;
usernameInput.value = "";
passwordInput.value = "";
data.innerHTML = `
<td id='${id}username'>${usernameInput.value}</td>
<td>${passwordInput.value}</td>
<td>
<button style="background-color: white; color: black; padding:2px; margin: 2px;">Edit</button>
<button style="background-color: red; color: white; padding:2px; margin: 2px;" onclick="deleteItem('${id}')">Delete</button>
</td>
`
}
usernameInput.addEventListener("keyup", (event) => {
console.log("text : ", usernameInput.value);
if (usernameInput.value.length < 4) {
isValid = false;
usernameError.innerText = "username must be greater than 4 character";
} else {
isValid = true;
usernameError.innerText = "";
}
});
passwordInput.addEventListener("keyup", (event) => {
console.log("text : ", passwordInput.value);
if (passwordInput.value.length < 4) {
isValid = false;
passwordError.innerText = "password must be greater than 4 character";
} else {
isValid = true;
passwordError.innerText = "";
}
});
document.getElementById("submit").addEventListener("click", (event) => {
console.log("inValid : ", isValid);
if (isValid) {
if (isEdit) {
editSubmitData();
return;
}
submitData();
}
});
function showPasswordClick() {
const buttonText = showButton.innerText;
if (buttonText == "show password") {
passwordInput.type = "text";
showButton.innerText = "hide password";
} else {
passwordInput.type = "password";
showButton.innerText = "show password";
}
}
</script>
</body>
</html>