How can I replace the whole list without adding new li’s to the list?
I think I should “reboot” it somehow, but I’m not sure how. I could just do it by filling empty li’s in the HTML file, but then I’ll have a problem when there are more or less items.
JavaScript
x
43
43
1
let objects = [
2
{
3
"id": 1,
4
"name": "broom",
5
"places": ["kitchen", "shop", "pharmacy"]
6
},
7
{
8
"id": 2,
9
"name": "wheels",
10
"places": ["park", "pool", "square"]
11
},
12
{
13
"id": 3,
14
"name": "wood",
15
"places": ["church", "garage", "bathroom"]
16
}
17
];
18
19
const nameObject = document.getElementById('objectName');
20
const nextBtn = document.getElementById('objectNext');
21
let objectIndex = 0;
22
23
nextBtn.onclick = function () {
24
objectIndex === objects.length - 1 ?
25
objectIndex = 0 :
26
objectIndex ++;
27
nameObject.innerHTML = objects[objectIndex].name;
28
objects[objectIndex].places.forEach(place => {
29
createPlaceListItem(place);
30
});
31
};
32
33
const createPlaceListItem = place => {
34
const $item = document.createElement(`li`);
35
$item.classList.add(`objectListItem`);
36
$item.innerHTML = place;
37
document.getElementById(`placeList`).appendChild($item);
38
};
39
40
nameObject.innerHTML = objects[objectIndex].name;
41
objects[objectIndex].places.forEach(place => {
42
createPlaceListItem(place);
43
});
JavaScript
1
5
1
<h4 id="objectName" class="objectName"></h4>
2
<ul class="objectList" id="placeList">
3
</ul>
4
5
<button class="objectNext" id="objectNext">next</button>
Thanks!
Advertisement
Answer
While using innerHTML
to clean the list is effective, using a proper removeChild
is not only arguably more idiomatic but also faster (although the performance will not matter for such small lists). It can be just:
JavaScript
1
2
1
while (list.firstChild) list.removeChild(list.firstChild);
2
Here is your code with that line:
JavaScript
1
44
44
1
let objects = [{
2
"id": 1,
3
"name": "broom",
4
"places": ["kitchen", "shop", "pharmacy"]
5
},
6
{
7
"id": 2,
8
"name": "wheels",
9
"places": ["park", "pool", "square"]
10
},
11
{
12
"id": 3,
13
"name": "wood",
14
"places": ["church", "garage", "bathroom"]
15
}
16
];
17
18
const nameObject = document.getElementById('objectName');
19
const nextBtn = document.getElementById('objectNext');
20
const list = document.getElementById(`placeList`);
21
let objectIndex = 0;
22
23
nextBtn.onclick = function() {
24
objectIndex === objects.length - 1 ?
25
objectIndex = 0 :
26
objectIndex++;
27
nameObject.innerHTML = objects[objectIndex].name;
28
while (list.firstChild) list.removeChild(list.firstChild);
29
objects[objectIndex].places.forEach(place => {
30
createPlaceListItem(place);
31
});
32
};
33
34
const createPlaceListItem = place => {
35
const $item = document.createElement(`li`);
36
$item.classList.add(`objectListItem`);
37
$item.innerHTML = place;
38
list.appendChild($item);
39
};
40
41
nameObject.innerHTML = objects[objectIndex].name;
42
objects[objectIndex].places.forEach(place => {
43
createPlaceListItem(place);
44
});
JavaScript
1
5
1
<h4 id="objectName" class="objectName"></h4>
2
<ul class="objectList" id="placeList">
3
</ul>
4
5
<button class="objectNext" id="objectNext">next</button>