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 > Event Delegation</ title >
8+ </ head >
9+ < body >
10+ < div id ="output "> </ div >
11+
12+ < script >
13+ const output = document . getElementById ( "output" ) ;
14+ output . innerHTML = `
15+
16+ <h1>JS DOM</h1>
17+ <button type="button" id="btn">Add More</button>
18+ <ul id="list">
19+
20+ <li>Who Are you?</li>
21+ <li>Shinra Tensei</li>
22+ <li>WOw</li>
23+ <li>Nicee</li>
24+
25+ </ul>
26+ ` ;
27+ const list = document . querySelector ( "#list" ) ;
28+ const items = [ ...list . querySelectorAll ( "li" ) ] ; //convert nodelist to array
29+ const btn = document . querySelector ( "#btn" ) ;
30+ function handleClick ( e ) {
31+ console . log ( e . target . nodeName ) ;
32+
33+ if ( e . target . nodeName . toLowerCase ( ) === "li" ) {
34+ console . log ( e . target . innerText ) ;
35+ }
36+ // e.target.style.display = "none";
37+ }
38+ // items.forEach((item) => {
39+ // item.addEventListener("click", handleClick);
40+ // });
41+ list . addEventListener ( "click" , handleClick ) ;
42+
43+ btn . addEventListener ( "click" , ( ) => {
44+ const items = list . querySelectorAll ( "li" ) ;
45+ const li = document . createElement ( "li" ) ;
46+ li . innerText = `item ${ items . length + 1 } ` ;
47+ list . append ( li ) ;
48+ } ) ;
49+ </ script >
50+ </ body >
51+ </ html >
You can’t perform that action at this time.
0 commit comments