Last active
July 19, 2023 03:47
-
-
Save joelethan/f25a0e10f87bb9bd3f0606f1771cb656 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DOM Manipulation session 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A. DOM Manipulation Part 1
DOM manipulation refers to the process of using JavaScript to interact with and modify the content, structure, and styles of a web page's Document Object Model (DOM). The DOM represents the web page as a tree of objects, where each object corresponds to an element, attribute, or text node in the HTML document. By manipulating the DOM, developers can create dynamic and interactive web applications. Let's illustrate DOM manipulation with practical examples and code:
1. Change Text Content of an element
In HTML
Suppose you have the following HTML element on your web page:
JavaScript
You can change the text content using DOM manipulation like this:
2. Add Elements to the web page Dynamically
In HTML
Suppose you want to add a new item to an unordered list on your web page:
JavaScript
You can use DOM manipulation to add a new item dynamically:
3. Respond to Events like a Button Click
In HTML
Suppose you have a button on your web page and want to show an alert when the button of a given id is clicked:
JavaScript
You can use DOM manipulation to attach an event listener and respond to the button click:
4. Change CSS Styles of elements Dynamically
In HTML
Suppose you want to change the background color of a specific element on your web page based on user input:
Among other elements, we have a div with id myDiv whose background color we want to change.
We want to pick the new color we will set from an Input element of id colorInput and set it as the background color, when a button of id changeColorBtn is clicked.
JavaScript
You can use DOM manipulation to change the style dynamically:
NB: