File tree Expand file tree Collapse file tree 3 files changed +56
-0
lines changed
95-Reducing DOM Manipulations Expand file tree Collapse file tree 3 files changed +56
-0
lines changed 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 name ="viewport " content ="width=device-width, initial-scale=1.0 " />
6+ < link rel ="stylesheet " href ="styles.css " />
7+ < title > Reducing DOM Manipulations App</ title >
8+ </ head >
9+ < body >
10+ < div class ="container ">
11+ < h1 > Reducing DOM Manipulations App</ h1 >
12+ < ul id ="listContainer ">
13+ <!-- List items will be dynamically added here -->
14+ </ ul >
15+ < button id ="addItemBtn "> Add Item</ button >
16+ </ div >
17+
18+ < script src ="script.js "> </ script >
19+ </ body >
20+ </ html >
Original file line number Diff line number Diff line change 1+ // Function to add a new list item
2+ function addItem ( ) {
3+ const listContainer = document . getElementById ( "listContainer" ) ;
4+ const newItem = document . createElement ( "li" ) ;
5+ newItem . textContent = `Item ${ listContainer . childElementCount + 1 } ` ;
6+
7+ // Add event listener to the new item
8+ newItem . addEventListener ( "click" , handleItemClick ) ;
9+
10+ // Append the new item to the list container
11+ listContainer . appendChild ( newItem ) ;
12+ }
13+
14+ // Function to handle item click (event delegation)
15+ function handleItemClick ( event ) {
16+ alert ( `You clicked on: ${ event . target . textContent } ` ) ;
17+ }
18+
19+ // Add event listener to the "Add Item" button
20+ document . getElementById ( "addItemBtn" ) . addEventListener ( "click" , addItem ) ;
Original file line number Diff line number Diff line change 1+ body {
2+ font-family : Arial, sans-serif;
3+ display : flex;
4+ align-items : center;
5+ justify-content : center;
6+ height : 100vh ;
7+ margin : 0 ;
8+ }
9+
10+ .container {
11+ text-align : center;
12+ }
13+
14+ button {
15+ padding : 5px 10px ;
16+ }
You can’t perform that action at this time.
0 commit comments