Skip to content

Commit c7dc1c3

Browse files
committed
docs: traversing & selecting dom elements
1 parent 18161e2 commit c7dc1c3

3 files changed

+171
-5
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ This advanced JavaScript course is designed for experienced developers looking t
2626
- 2.3. [Mixins and Composition](#mixins-and-composition)
2727
- 2.4. [Private and Static Members](#private-and-static-members)
2828

29-
### [Module 3: Advanced DOM Manipulation](#module-3-advanced-dom-manipulation)
30-
3.1. [Traversing and Selecting DOM Elements](#traversing-and-selecting-dom-elements)
31-
3.2. [Creating and Modifying DOM Elements](#creating-and-modifying-dom-elements)
32-
3.3. [Event Handling and Delegation](#event-handling-and-delegation)
33-
3.4. [Web APIs and AJAX](#web-apis-and-ajax)
29+
### [Module 3: Advanced DOM Manipulation](./module-3-advanced-dom-manipulation.md)
30+
31+
- 3.1. [Traversing and Selecting DOM Elements](./module-3.1-traversing-and-selecting-dom-elements.md)
32+
- 3.2. [Creating and Modifying DOM Elements](#creating-and-modifying-dom-elements)
33+
- 3.3. [Event Handling and Delegation](#event-handling-and-delegation)
34+
- 3.4. [Web APIs and AJAX](#web-apis-and-ajax)
3435

3536
### [Module 4: Error Handling and Debugging](#module-4-error-handling-and-debugging)
3637
4.1. [Error Handling Best Practices](#error-handling-best-practices)

module-3-advanced-dom-manipulation.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
### Module 3: Advanced DOM Manipulation
2+
3+
**Description:**
4+
5+
Module 3 dives into advanced techniques for manipulating the Document Object Model (DOM) using JavaScript. You'll learn how to create interactive and dynamic web pages by accessing and modifying the DOM effectively.
6+
7+
**Topics Covered:**
8+
9+
### 3.1. Selecting and Modifying DOM Elements
10+
11+
- **Query Selectors:** Explore advanced selection methods, including `querySelector`, `querySelectorAll`, and `getElementsBy...`.
12+
13+
- **Modifying Elements:** Learn how to modify content, attributes, and styles of DOM elements.
14+
15+
- **Creating and Appending Elements:** Dynamically create and insert elements into the DOM.
16+
17+
- **Removing Elements:** Remove unwanted elements from the DOM.
18+
19+
**Examples:**
20+
21+
- Update the content of specific elements.
22+
- Create and insert elements dynamically.
23+
- Remove elements from the DOM.
24+
25+
### 3.2. Event Handling
26+
27+
- **Event Listeners:** Implement event listeners to respond to user interactions, like clicks, keypresses, and more.
28+
29+
- **Event Bubbling and Delegation:** Understand event propagation and how to efficiently handle events on multiple elements.
30+
31+
- **Preventing Default Actions:** Learn how to prevent the default behavior of certain events, like form submission or link clicks.
32+
33+
- **Event Object:** Utilize the event object to access information about the event, such as the target element or keycodes.
34+
35+
**Examples:**
36+
37+
- Create interactive buttons that respond to clicks.
38+
- Implement a form with custom validation using event listeners.
39+
40+
### 3.3. Animation and Transitions
41+
42+
- **CSS Transitions:** Apply smooth transitions to DOM elements using CSS properties.
43+
44+
- **JavaScript Animations:** Create animations by modifying element properties over time using JavaScript.
45+
46+
- **RequestAnimationFrame:** Use the `requestAnimationFrame` method for smoother and more efficient animations.
47+
48+
**Examples:**
49+
50+
- Create a simple image slider with smooth transitions.
51+
- Implement an animated progress bar.
52+
53+
### 3.4. DOM Manipulation Libraries
54+
55+
- **Introduction to Libraries:** Explore popular libraries like jQuery and how they simplify DOM manipulation.
56+
57+
- **Library Usage:** Learn how to include and use a library for efficient DOM manipulation.
58+
59+
- **Benefits and Considerations:** Understand the advantages and limitations of using a library.
60+
61+
**Examples:**
62+
63+
- Implement a simple feature using jQuery.
64+
- Compare vanilla JavaScript with library-based DOM manipulation.
65+
66+
**Practical Application:**
67+
68+
The concepts and techniques covered in this module can be applied to enhance the interactivity and user experience of web applications. You'll be able to create dynamic web pages, handle user interactions, and add animations and transitions.
69+
70+
**Key Takeaways:**
71+
72+
Mastering advanced DOM manipulation techniques is crucial for creating modern and interactive web applications. You'll gain the skills to make web pages more engaging and responsive, improving user satisfaction and retention.
73+
74+
This module will empower you to build web applications that respond to user interactions, provide smooth animations, and create a dynamic user experience.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#### 3.1. Traversing and Selecting DOM Elements
2+
3+
In this section, we'll explore advanced techniques for selecting and manipulating DOM elements using JavaScript.
4+
5+
##### Query Selectors
6+
7+
JavaScript provides several methods for selecting DOM elements, with the most versatile being query selectors. Query selectors allow you to select elements using CSS-style selectors.
8+
9+
**Example:**
10+
11+
```html
12+
<!DOCTYPE html>
13+
<html>
14+
<body>
15+
<div id="container">
16+
<p class="highlight">This is a paragraph.</p>
17+
<p>Another paragraph.</p>
18+
</div>
19+
</body>
20+
</html>
21+
```
22+
23+
```javascript
24+
// Select by ID
25+
const container = document.querySelector('#container');
26+
27+
// Select by class
28+
const highlightParagraph = container.querySelector('.highlight');
29+
30+
// Select by tag
31+
const paragraphs = container.querySelectorAll('p');
32+
```
33+
34+
In this example, we use `querySelector` to select the `#container` element by ID, and then use it to find the paragraph with the class `.highlight`. We also use `querySelectorAll` to select all `p` elements within the container.
35+
36+
##### Modifying Elements
37+
38+
Once you've selected elements, you can modify their content, attributes, and styles.
39+
40+
**Example:**
41+
42+
```javascript
43+
// Modify text content
44+
highlightParagraph.textContent = 'This is a modified paragraph.';
45+
46+
// Modify attributes
47+
highlightParagraph.setAttribute('style', 'font-weight: bold;');
48+
49+
// Modify styles
50+
highlightParagraph.style.backgroundColor = 'yellow';
51+
```
52+
53+
In this example, we modify the text content of the `.highlight` paragraph, set a new style attribute, and change the background color using the `textContent`, `setAttribute`, and `style` properties.
54+
55+
##### Creating and Appending Elements
56+
57+
You can dynamically create and insert elements into the DOM.
58+
59+
**Example:**
60+
61+
```javascript
62+
// Create a new paragraph element
63+
const newParagraph = document.createElement('p');
64+
newParagraph.textContent = 'This is a new paragraph.';
65+
66+
// Append the new paragraph to the container
67+
container.appendChild(newParagraph);
68+
```
69+
70+
Here, we create a new paragraph element, set its content, and then append it to the `#container` element using `appendChild`.
71+
72+
##### Removing Elements
73+
74+
You can remove unwanted elements from the DOM.
75+
76+
**Example:**
77+
78+
```javascript
79+
// Remove an element
80+
const paragraphToRemove = container.querySelector('p');
81+
container.removeChild(paragraphToRemove);
82+
```
83+
84+
In this example, we select a paragraph element and then use the `removeChild` method to remove it from the DOM.
85+
86+
#### Benefits
87+
88+
- Query selectors provide powerful and flexible methods for selecting DOM elements using CSS-style syntax.
89+
- Modifying elements, creating new ones, and removing unwanted ones allows you to dynamically manipulate the content and structure of your web page.
90+
91+
These techniques are fundamental for creating interactive and dynamic web pages. Whether you want to build interactive forms, create a content slider, or implement a more user-friendly interface, understanding how to traverse and select DOM elements is crucial.

0 commit comments

Comments
 (0)