Page 185 - Web_Application_v2.0_C12_Fb
P. 185

// Change text content
                    document.getElementById('CHANGETEXT').addEventListener('click', function() {

                    document.getElementById('TITLE').textContent = 'Welcome to JavaScript DOM!';
                    });
                    // Add a new div

                    document.getElementById('ADDDIV').addEventListener('click', function() {
                    const newDiv = document.createElement('div');
                    newDiv.textContent = 'This is a newly added element!';
                    document.getElementById('CONTAINER').appendChild(newDiv);
                    });

                    </SCRIPT>
                    </BODY>
                    </HTML>
                 Output:
















                 When you click on the Change Text button the text of the web page changes from Hello, World! To Welcome
                 to JavaScript DOM!

















                 When you click on the Add Div button, a new div element is added to the web page.



















                                                                                                  JavaScript Part 2  183
   180   181   182   183   184   185   186   187   188   189   190