File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+ < head >
4+ < meta charset ="UTF-8 " />
5+ < meta http-equiv ="X-UA-Compatible " content ="IE=edge " />
6+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 " />
7+ < title > Looping over DOM Elements</ title >
8+ </ head >
9+ < body >
10+ < div id ="output "> </ div >
11+
12+ < script >
13+ const output = document . getElementById ( "output" ) ;
14+
15+ output . innerHTML = `
16+ <h1>Hello DOM!</h1>
17+
18+ <ul id="list">
19+ <li>One</li>
20+ <li>Two</li>
21+ <li>Three</li>
22+ <li>Four</li>
23+ </ul>
24+ ` ;
25+ const listItems = document . querySelectorAll ( "#list li" ) ;
26+ //By using for loop
27+ for ( let i = 0 ; i < listItems . length ; i ++ ) {
28+ // console.log(listItems[i]);
29+ console . log ( listItems [ i ] . innerText ) ;
30+ }
31+ console . log ( "---------" ) ;
32+
33+ //using for of loop
34+ for ( const item of listItems ) {
35+ console . log ( item ) ;
36+ }
37+ console . log ( "---------" ) ;
38+ //using destructuring+forEach loop
39+ [ ...listItems ] . forEach ( ( item ) => console . log ( item ) ) ;
40+ console . log ( "---------" ) ;
41+ //using Array.from+ForEach loop
42+ Array . from ( listItems ) . forEach ( ( item ) => console . log ( item . textContent ) ) ;
43+ </ script >
44+ </ body >
45+ </ html >
You can’t perform that action at this time.
0 commit comments