-
Notifications
You must be signed in to change notification settings - Fork 0
/
Loop-on-Node-List.html
61 lines (40 loc) · 1007 Bytes
/
Loop-on-Node-List.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
<p>Loop on NodeList (Nodelist is related DOM)</p>
<ul>
<li class="nama">Arjuna</li>
<li class="nama">Nakula</li>
<li class="nama">Sadewa</li>
</ul>
<ul class="list">
<li>Satu</li>
<li>Dua</li>
<li>Tiga</li>
</ul>
<script>
const liNama = document.querySelectorAll('.nama') // collect all tags with class '.nama'
liNama.forEach(n => console.log(n.textContent));
// output:
// Arjuna
// Nakula
// Sadewa
//pada browser lama loop NodeList dengan forEach tidak bisa, karena bukan Array
//pada browser baru loop NodeList bisa dengan forEach
for (n of liNama) {
console.log(n.innerHTML);
}
// output:
// Arjuna
// Nakula
// Sadewa
let nl = document.querySelector(".list").getElementsByTagName("li");
let arr = [...nl].map(m => m.textContent);
console.log(arr);
// output:
// ["Satu", "Dua", "Tiga"]
let arr1 = [];
for(let i=0; i<nl.length; i++){
arr1.push(nl[i].textContent);
}
console.log(arr1);
// output:
// ["Satu", "Dua", "Tiga"]
</script>