-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdynamic-table-row.html
96 lines (92 loc) · 2.29 KB
/
dynamic-table-row.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>How to add & remove table rows</title>
<link
href="https://fonts.googleapis.com/css2?family=Roboto:wght@100;400&display=swap"
rel="stylesheet"
/>
<style>
body {
font-family: "Roboto", sans-serif;
}
h1 {
text-align: center;
}
table,
form {
width: 500px;
margin: 20px auto;
}
table {
border-collapse: collapse;
text-align: center;
}
table td,
table th {
border: solid 1px black;
}
label,
input {
display: block;
margin: 10px 0;
font-size: 20px;
}
button {
display: block;
}
</style>
</head>
<body>
<h1>Dynamically Add & Remove Table Rows</h1>
<form>
<div class="input-row">
<label for="url">Url</label>
<input type="url" name="url" id="url" />
</div>
<div class="input-row">
<label for="website">Website Name</label>
<input type="text" name="website" id="website" />
</div>
<button>Submit</button>
</form>
<table>
<thead>
<tr>
<th>Url</th>
<th>Website</th>
<th></th>
</tr>
</thead>
<tbody></tbody>
</table>
<script>
const formEl = document.querySelector("form");
const tbodyEl = document.querySelector("tbody");
const tableEl = document.querySelector("table");
function onAddWebsite(e) {
e.preventDefault();
const website = document.getElementById("website").value;
const url = document.getElementById("url").value;
tbodyEl.innerHTML += `
<tr>
<td>${url}</td>
<td>${website}</td>
<td><button class="deleteBtn">Delete</button></td>
</tr>
`;
}
function onDeleteRow(e) {
if (!e.target.classList.contains("deleteBtn")) {
return;
}
const btn = e.target;
btn.closest("tr").remove();
}
formEl.addEventListener("submit", onAddWebsite);
tableEl.addEventListener("click", onDeleteRow);
</script>
</body>
</html>