Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 81536e4

Browse files
Added dom manipulation
1 parent 6267bd0 commit 81536e4

File tree

2 files changed

+173
-11
lines changed

2 files changed

+173
-11
lines changed

‎README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,17 @@ This repository contains Vanilla JavaScript topics and their notes to learn from
5656
- [Function arguments and return values](./docs/functions.md#-function-arguments-and-return-values)
5757
- [Function Scope and closures](./docs/functions.md#-function-scope-and-closures)
5858
- [Arrow functions](./docs/functions.md#-arrow-functions)
59-
3. DOM Manipulation:
60-
- Selecting elements
61-
- Creating elements
62-
- Adding elements
63-
- Modifying elements
64-
- Modifying attributes
65-
- Changing styles
66-
- Removing elements
67-
- Event Handling:
68-
- Attaching event listeners to elements
69-
- Handling common events and custom events
59+
3. [DOM Manipulation:](./docs/dom-manipulation.md)
60+
- [Selecting elements](./docs/dom-manipulation.md#-selecting-elements)
61+
- [Creating elements](./docs/dom-manipulation.md#-creating-elements)
62+
- [Adding elements](./docs/dom-manipulation.md#-adding-elements)
63+
- [Modifying elements](./docs/dom-manipulation.md#-modifying-elements)
64+
- [Modifying attributes](./docs/dom-manipulation.md#-modifying-attributes)
65+
- [Changing styles](./docs/dom-manipulation.md#-changing-styles)
66+
- [Removing elements](./docs/dom-manipulation.md#-removing-elements)
67+
- [Event Handling](./docs/dom-manipulation.md#-event-handling)
68+
- [Attaching event listeners](./docs/dom-manipulation.md#-attaching-event-listeners)
69+
- [Handling events](./docs/dom-manipulation.md#-handling-events)
7070

7171
### Intermediate Concepts
7272
1. Objects:

‎docs/dom-manipulation.md

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /