Skip to content

Commit 150ac24

Browse files
class : list binding exercise
1 parent d0477f1 commit 150ac24

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

practice/class-ListBinding/app.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class ListBinding {
2+
constructor(element) {
3+
this.listElement = element;
4+
this.textList = [];
5+
}
6+
7+
// Make an <li>text</li> element/tag
8+
static createListItem(text) {
9+
const li = document.createElement("li");
10+
li.textContent = text;
11+
return li;
12+
}
13+
14+
update() {
15+
// Remove all existing <li> elements/tags
16+
while (this.listElement.firstChild) {
17+
this.listElement.removeChild(this.listElement.firstChild);
18+
}
19+
20+
// Fill <ul>/<ol> tag with <li>
21+
for (const text of this.textList) {
22+
this.listElement.appendChild(ListBinding.createListItem(text));
23+
}
24+
}
25+
26+
add(text) {
27+
this.textList.push(text);
28+
this.update();
29+
}
30+
31+
remove(index) {
32+
this.textList.splice(index, 1);
33+
this.update();
34+
}
35+
}
36+
37+
const myList = document.getElementById("myList");
38+
const listBinding = new ListBinding(myList);

practice/class-ListBinding/index.html

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Classes: HTML List Binding - JS</title>
7+
</head>
8+
<body>
9+
<h2 id="title">Classes: HTML List Binding</h2>
10+
<ul id="myList">
11+
<!-- to be filled by JS -->
12+
</ul>
13+
14+
<script src="app.js"></script>
15+
</body>
16+
</html>

0 commit comments

Comments
 (0)