@@ -28,21 +28,21 @@ For example: document.getElementById("myId") gets a node, and you can change its
28
28
## Accessing and Manipulating the DOM with JavaScript
29
29
30
30
### 1. Selecting Elements
31
- ` document.getElementById("myId"); `
32
- ` document.querySelector(".myClass"); `
33
- ` document.querySelectorAll("div"); ` <!-- NodeList of all divs -->
31
+ - ` document.getElementById("myId"); `
32
+ - ` document.querySelector(".myClass"); `
33
+ - ` document.querySelectorAll("div"); ` <!-- NodeList of all divs -->
34
34
35
35
### 2. Changing Styles
36
- ` const title = document.getElementById("title"); `
37
- ` title.style.backgroundColor = "blue"; `
38
- ` title.style.color = "white"; `
39
- ` title.style.padding = "12px"; `
40
- ` title.style.borderRadius = "15px"; `
36
+ - ` const title = document.getElementById("title"); `
37
+ - ` title.style.backgroundColor = "blue"; `
38
+ - ` title.style.color = "white"; `
39
+ - ` title.style.padding = "12px"; `
40
+ - ` title.style.borderRadius = "15px"; `
41
41
42
42
### 3. Changing Content
43
- ` title.textContent = "DOM DOM DOM"; `
44
- ` title.innerHTML = "<span>DOM</span>"; `
45
- ` title.innerText = "DOM DOM"; ` <!-- Only shows visibile text -->
43
+ - ` title.textContent = "DOM DOM DOM"; `
44
+ - ` title.innerHTML = "<span>DOM</span>"; `
45
+ - ` title.innerText = "DOM DOM"; ` <!-- Only shows visibile text -->
46
46
47
47
#### Difference Between textContent, innerHTML, innerText
48
48
@@ -56,12 +56,17 @@ For example: document.getElementById("myId") gets a node, and you can change its
56
56
- 💡 ** Note:** ` innerText ` is affected by CSS styles (e.g., ` display: none ` ), while ` textContent ` is not.
57
57
58
58
### 4. Changing Attributes
59
- ` title.setAttribute("class", "heading common"); `
60
- ` title.getAttribute("class"); `
61
- ` title.removeAttribute("class"); `
59
+ - ` title.setAttribute("class", "heading common"); `
60
+ - ` title.getAttribute("class"); `
61
+ - ` title.removeAttribute("class"); `
62
62
63
63
### 5. Adding/Removing Elements
64
-
64
+ ``` javascript
65
+ const new = document .createElement (" div" );
66
+ new .textContent = " I'm new!" ;
67
+ document .body .appendChild (new );
68
+ document .body .removeChild (new );
69
+ ```
65
70
66
71
### 6. Event Listeners
67
72
``` javascript
0 commit comments