|
| 1 | +## ⚑ DOM Manipulation: |
| 2 | +DOM is stands for Document Object Model. A tree-structured representation of an HTML document, providing a programmatic interface to access and manipulate elements, attributes, and content. |
| 3 | + |
| 4 | +*DOM Manipulation is the process of dynamically modifying the structure, content, or style of an HTML document using JavaScript.* |
| 5 | + |
| 6 | +### ☴ Overview: |
| 7 | +1. [Selecting elements](#-selecting-elements) |
| 8 | +2. [Creating elements](#-creating-elements) |
| 9 | +3. [Adding elements](#-adding-elements) |
| 10 | +4. [Modifying elements](#-modifying-elements) |
| 11 | + - [Modifying attributes](#-modifying-attributes) |
| 12 | + - [Changing styles](#-changing-styles) |
| 13 | +5. [Removing elements](#-removing-elements) |
| 14 | +6. [Event Handling](#-event-handling) |
| 15 | + - [Attaching event listeners](#-attaching-event-listeners) |
| 16 | + - [Handling events](#-handling-events) |
| 17 | + |
| 18 | +### ✦ Selecting elements: |
| 19 | +To select a HTML element via JavaScript for DOM manipulation. |
| 20 | + |
| 21 | +*Syntax:* |
| 22 | +```javascript |
| 23 | +document.getElementById(id); |
| 24 | +document.getElementsByTagName(tagName); |
| 25 | +document.getElementsByClassName(className); |
| 26 | +document.querySelector(selector); |
| 27 | +document.querySelectorAll(selector); |
| 28 | +``` |
| 29 | + |
| 30 | +```javascript |
| 31 | +let elementById = document.getElementById("myElement"); |
| 32 | +let elementsByTagName = document.getElementsByTagName("p"); |
| 33 | +let elementsByClassName = document.getElementsByClassName("myClass"); |
| 34 | +let firstElementBySelector = document.querySelector("div#submit-button"); |
| 35 | +let allElementsBySelector = document.querySelectorAll("div.myClass"); |
| 36 | +``` |
| 37 | + |
| 38 | +### ✦ Creating elements: |
| 39 | +To create an HTML element using DOM manipulation. |
| 40 | + |
| 41 | +*Syntax:* |
| 42 | +```javascript |
| 43 | +let newElement = document.createElement(tagName); |
| 44 | +``` |
| 45 | + |
| 46 | +```javascript |
| 47 | +let newParagraph = document.createElement("p"); |
| 48 | +``` |
| 49 | + |
| 50 | +### ✦ Adding elements: |
| 51 | +To add create HTML element in the DOM tree. |
| 52 | + |
| 53 | +*Syntax:* |
| 54 | +```javascript |
| 55 | +parentElement.appendChild(childElement); |
| 56 | +parentElement.insertBefore(childElement, referenceElement); |
| 57 | +``` |
| 58 | + |
| 59 | +```javascript |
| 60 | +let container = document.getElementById("container"); |
| 61 | +container.appendChild(newParagraph); |
| 62 | +``` |
| 63 | + |
| 64 | +### ✦ Modifying elements: |
| 65 | +To modify the HTML element as changing their properties or removing. |
| 66 | + |
| 67 | +### ✦ Modifying attributes: |
| 68 | +To modifying the attributes and properties of the HTML elements. |
| 69 | + |
| 70 | +*Syntax:* |
| 71 | +```javascript |
| 72 | +element.setAttribute(attributeName, attributeValue); |
| 73 | +element.getAttribute(attributeName); |
| 74 | +``` |
| 75 | + |
| 76 | +```javascript |
| 77 | +let link = document.createElement("a"); |
| 78 | +link.setAttribute("href", "https://arathinai.blogspot.com"); |
| 79 | +link.setAttribute("target", "_blank"); |
| 80 | +``` |
| 81 | + |
| 82 | +### ✦ Changing styles: |
| 83 | +To changing the default or initial style of an element. |
| 84 | + |
| 85 | +*Syntax:* |
| 86 | +```javascript |
| 87 | +element.style.propertyName = propertyValue; |
| 88 | +``` |
| 89 | + |
| 90 | +```javascript |
| 91 | +let heading = document.querySelector("h1"); |
| 92 | +heading.style.color = "blue"; |
| 93 | +heading.style.fontSize = "24px"; |
| 94 | +``` |
| 95 | + |
| 96 | +### ✦ Removing elements: |
| 97 | +To remove an element from the DOM tree. |
| 98 | + |
| 99 | +*Syntax:* |
| 100 | +```javascript |
| 101 | +parentElement.removeChild(childElement); |
| 102 | +``` |
| 103 | + |
| 104 | +```javascript |
| 105 | +container.removeChild(newParagraph); |
| 106 | +``` |
| 107 | + |
| 108 | +### ✦ Event Handling: |
| 109 | +*Events:* Actions that occur in a web page, such as clicks, key presses, or mouse movements. |
| 110 | + |
| 111 | +### ✦ Attaching event listeners: |
| 112 | +To listen for an event occurrence of an element, That need to be attach specific type of event. |
| 113 | + |
| 114 | +*Syntax:* |
| 115 | +```javascript |
| 116 | +element.addEventListener(eventType, eventHandler); |
| 117 | +``` |
| 118 | + |
| 119 | +```javascript |
| 120 | +let button = document.getElementById("myButton"); |
| 121 | +button.addEventListener("click", function() { |
| 122 | + console.log("Button clicked!"); |
| 123 | +}); |
| 124 | +``` |
| 125 | + |
| 126 | +### ✦ Handling events: |
| 127 | +*Event Handling:* The process of responding to events in a web page using JavaScript. |
| 128 | + |
| 129 | +**Common events:** |
| 130 | +- click |
| 131 | +- mouseover |
| 132 | +- mouseout |
| 133 | +- keydown |
| 134 | +- keyup |
| 135 | +- submit |
| 136 | +- change |
| 137 | +- load |
| 138 | +- resize |
| 139 | +- scroll |
| 140 | + |
| 141 | +**Custom events:** |
| 142 | +Create own events using dispatchEvent() and addEventListener(). |
| 143 | + |
| 144 | +```javascript |
| 145 | +// Create a custom event |
| 146 | +let myCustomEvent = new Event("myCustomEvent"); |
| 147 | + |
| 148 | +// Dispatch the event |
| 149 | +document.dispatchEvent(myCustomEvent); |
| 150 | + |
| 151 | +// Listen for the event |
| 152 | +document.addEventListener("myCustomEvent", function() { |
| 153 | + console.log("Custom event fired!"); |
| 154 | +}); |
| 155 | +``` |
| 156 | + |
| 157 | +--- |
| 158 | +[⇪ To Top](#-dom-manipulation) |
| 159 | + |
| 160 | +[❮ Previous Topic](./functions.md)   [Next Topic ❯](./objects.md) |
| 161 | + |
| 162 | +[⌂ Goto Home Page](../README.md) |
0 commit comments