|
1 | | -<ComingSoon /> |
| 1 | +--- |
| 2 | +title: "DOM Manipulation" |
| 3 | +sidebar_label: "5. DOM Manipulation" |
| 4 | +sidebar_position: 5 |
| 5 | +description: "Learn how to use JavaScript to change your HTML and CSS on the fly." |
| 6 | +--- |
| 7 | + |
| 8 | +The **DOM (Document Object Model)** is the map that the browser creates of your HTML. JavaScript uses this map to find, change, add, or delete things on your web page without refreshing it. |
| 9 | + |
| 10 | +Think of the DOM as a **Tree**. Every HTML tag is a branch or a leaf that JavaScript can grab onto. |
| 11 | + |
| 12 | +## 1. Selecting Elements |
| 13 | + |
| 14 | +Before you can change something, you have to "find" it. JavaScript provides several ways to grab elements from your HTML. |
| 15 | + |
| 16 | +| Method | Use case | Example | |
| 17 | +| :--- | :--- | :--- | |
| 18 | +| `getElementById()` | To find a single, unique element. | `document.getElementById('header')` | |
| 19 | +| `querySelector()` | To find the first element that matches a CSS selector. | `document.querySelector('.btn')` | |
| 20 | +| `querySelectorAll()` | To find EVERY element that matches. | `document.querySelectorAll('p')` | |
| 21 | + |
| 22 | +## 2. Changing the Content |
| 23 | + |
| 24 | +Once you have grabbed an element, you can change what is inside it. |
| 25 | + |
| 26 | +```javascript |
| 27 | +// 1. Find it |
| 28 | +const myTitle = document.getElementById('main-title'); |
| 29 | + |
| 30 | +// 2. Change it |
| 31 | +myTitle.innerText = "Welcome to CodeHarborHub!"; |
| 32 | +myTitle.style.color = "blue"; // You can even change CSS! |
| 33 | +``` |
| 34 | + |
| 35 | +## 3. Listening for Events |
| 36 | + |
| 37 | +This is how we make buttons actually *do* things. We use an **Event Listener** to "listen" for user actions like clicks, typing, or scrolling. |
| 38 | + |
| 39 | +```javascript |
| 40 | +const btn = document.querySelector('.my-button'); |
| 41 | + |
| 42 | +// "When the button is clicked, run this function" |
| 43 | +btn.addEventListener('click', () => { |
| 44 | + alert("Button was clicked!"); |
| 45 | +}); |
| 46 | +``` |
| 47 | + |
| 48 | +## 4. Creating New Elements |
| 49 | + |
| 50 | +You aren't stuck with the HTML you started with. You can create brand new elements out of thin air. |
| 51 | + |
| 52 | +```javascript |
| 53 | +// 1. Create it |
| 54 | +const newPara = document.createElement('p'); |
| 55 | + |
| 56 | +// 2. Add content |
| 57 | +newPara.innerText = "I was created by JavaScript!"; |
| 58 | + |
| 59 | +// 3. Put it on the page |
| 60 | +document.body.appendChild(newPara); |
| 61 | +``` |
| 62 | + |
| 63 | +## Let's Practice: The Dark Mode Toggle |
| 64 | + |
| 65 | +Let's build a simple feature that changes the background color when a button is clicked. |
| 66 | + |
| 67 | +**HTML:** |
| 68 | + |
| 69 | +```html |
| 70 | +<button id="theme-btn">Switch Theme</button> |
| 71 | +``` |
| 72 | + |
| 73 | +**JavaScript (`script.js`):** |
| 74 | + |
| 75 | +```javascript |
| 76 | +const themeBtn = document.getElementById('theme-btn'); |
| 77 | + |
| 78 | +themeBtn.addEventListener('click', () => { |
| 79 | + // Check the current color |
| 80 | + if (document.body.style.backgroundColor === "black") { |
| 81 | + document.body.style.backgroundColor = "white"; |
| 82 | + document.body.style.color = "black"; |
| 83 | + } else { |
| 84 | + document.body.style.backgroundColor = "black"; |
| 85 | + document.body.style.color = "white"; |
| 86 | + } |
| 87 | +}); |
| 88 | +``` |
| 89 | + |
| 90 | +## 5. Common DOM Properties |
| 91 | + |
| 92 | +<Tabs> |
| 93 | + <TabItem value="text" label="Text & HTML" default> |
| 94 | + * `.innerText`: Changes just the text. |
| 95 | + * `.innerHTML`: Changes the text AND allows you to add HTML tags. |
| 96 | + </TabItem> |
| 97 | + <TabItem value="classes" label="Classes"> |
| 98 | + It is better to change classes than individual styles: |
| 99 | + * `element.classList.add('active')` |
| 100 | + * `element.classList.remove('hidden')` |
| 101 | + * `element.classList.toggle('dark-mode')` |
| 102 | + </TabItem> |
| 103 | +</Tabs> |
| 104 | + |
| 105 | +:::info Don't Over-CSS in JS |
| 106 | +Avoid writing too much `element.style.color = "red"` in your JavaScript. Instead, define a class like `.error { color: red; }` in your CSS file and use JavaScript to `.add('error')`. This keeps your logic and design separate! |
| 107 | +::: |
0 commit comments