Skip to content

Commit a3836f7

Browse files
committed
complited Js tutorials
1 parent c829bd7 commit a3836f7

File tree

4 files changed

+401
-0
lines changed

4 files changed

+401
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
---
2+
sidebar_position: 5
3+
title: DOM Manipulation
4+
sidebar_label: DOM Manipulation
5+
description: "Learn how to use JavaScript to select, change, and animate HTML elements in real-time."
6+
---
7+
8+
Up until now, your JavaScript has been living inside the "Console." It’s been doing math and storing variables, but the user can't see that.
9+
10+
**DOM Manipulation** is how JavaScript talks to your HTML.
11+
12+
## What is the DOM?
13+
14+
**DOM** stands for **Document Object Model**.
15+
16+
When a browser loads your HTML file, it converts it into a "Tree" structure. Every tag (`<h1>`, `<div>`, `<p>`) is a "Branch" or a "Leaf" on that tree. JavaScript can climb this tree, find a specific leaf, and change its color, text, or even delete it!
17+
18+
## Step 1: Selecting Elements (Finding the Target)
19+
20+
Before you can change something, you have to "find" it. Imagine you are a sniper; you need a target.
21+
22+
In modern JS, we use two main "Scanners":
23+
24+
### 1. `document.getElementById('id-name')`
25+
26+
The fastest way to find a **single, unique** element.
27+
28+
```javascript
29+
const myTitle = document.getElementById('main-title');
30+
```
31+
32+
### 2. `document.querySelector('.class-name')`
33+
34+
The "Swiss Army Knife." It can find anything using CSS selectors (tags, classes, or IDs).
35+
36+
```javascript
37+
const myButton = document.querySelector('.submit-btn');
38+
const firstParagraph = document.querySelector('p');
39+
```
40+
41+
## Step 2: Changing Elements (The Transformation)
42+
43+
Once you have grabbed the element and stored it in a variable, you can change it!
44+
45+
### Changing Text
46+
47+
```javascript
48+
const heading = document.querySelector('h1');
49+
heading.innerText = "Hello CodeHarborHub!"; // Changes the text
50+
```
51+
52+
### Changing Style
53+
54+
```javascript
55+
const box = document.querySelector('.box');
56+
box.style.backgroundColor = "blue"; // Changes the CSS
57+
box.style.borderRadius = "50%";
58+
```
59+
60+
### Adding/Removing Classes
61+
62+
Instead of changing styles one by one, professionals usually just swap classes.
63+
64+
```javascript
65+
const card = document.querySelector('.card');
66+
card.classList.add('active'); // Adds the .active CSS class
67+
card.classList.remove('hidden'); // Removes the .hidden CSS class
68+
```
69+
70+
## Interactive Lab: The "Shape Shifter"
71+
72+
In this CodePen, notice how JavaScript "grabs" the box and changes its properties when you edit the code.
73+
74+
<CodePenEmbed
75+
title="JS DOM Manipulation Practice"
76+
penId="JoRYRKq"
77+
/>
78+
79+
### Challenge Tasks:
80+
81+
1. Find the line `box.style.backgroundColor = "red";` and change it to `green`.
82+
2. Use `box.innerText = "I have been hacked!";` to change the text inside.
83+
3. Try changing the `width` of the box to `300px` using `.style.width`.
84+
85+
## The "Workflow" Secret
86+
87+
Every time you want to make a change on a website, you follow these **3 Steps**:
88+
89+
1. **SELECT:** `const element = document.querySelector(...)`
90+
2. **LISTEN:** (We will learn this in the next lesson!)
91+
3. **CHANGE:** `element.style.color = "blue"`
92+
93+
## Summary Checklist
94+
95+
* [x] I know that **DOM** stands for Document Object Model.
96+
* [x] I can find an element using `getElementById` or `querySelector`.
97+
* [x] I can change the text of an element using `.innerText`.
98+
* [x] I understand that I can edit CSS directly using `.style`.
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
---
2+
sidebar_position: 6
3+
title: "Events: Making Your Site React"
4+
sidebar_label: Events
5+
description: "Learn how to listen for user actions like clicks, keypresses, and scrolls."
6+
---
7+
8+
If **DOM Manipulation** is the ability to change the page, then **Events** are the "Triggers" that tell JavaScript *when* to make those changes.
9+
10+
An Event is simply a signal that something has happened. Your job as a developer is to "Listen" for those signals.
11+
12+
## The Event Listener (The Watchman)
13+
14+
To react to an event, we use a special method called `addEventListener`. Think of it as hiring a 24/7 watchman for a specific button. You tell the watchman: *"Watch this button. **If** it gets clicked, **run** this function."*
15+
16+
### The Syntax:
17+
18+
```javascript
19+
const btn = document.querySelector('#myButton');
20+
21+
// element.addEventListener('event_type', function_to_run);
22+
btn.addEventListener('click', function() {
23+
alert("Button was clicked! 🎉");
24+
});
25+
```
26+
27+
## Common Types of Events
28+
29+
There are hundreds of events, but you will use these "Big Four" almost every day:
30+
31+
### 1. Click (`click`)
32+
33+
Triggered when the user clicks (or taps) an element.
34+
35+
* **Use for:** Submitting forms, opening menus, or liking a post.
36+
37+
### 2. Input (`input`)
38+
39+
Triggered every time a user types a single character into an input box.
40+
41+
* **Use for:** Real-time search or checking password strength.
42+
43+
### 3. Mouse Over (`mouseover`)
44+
45+
Triggered when the mouse pointer enters the area of an element.
46+
47+
* **Use for:** Showing tooltips or changing image highlights.
48+
49+
### 4. Submit (`submit`)
50+
51+
Triggered when a user sends a form.
52+
53+
* **Important:** We usually use `event.preventDefault()` here so the page doesn't refresh!
54+
55+
## Interactive Lab: The Event Dashboard
56+
57+
Open this CodePen and move your mouse around. Notice how the "Event Log" updates instantly based on what you are doing!
58+
59+
<CodePenEmbed
60+
title="JS Event Listeners Practice"
61+
penId="gbwawWg"
62+
/>
63+
64+
### Challenge Tasks:
65+
66+
1. Find the `mouseover` event in the JS tab and change the color to `orange`.
67+
2. Add a new listener for `dblclick` (double click) that changes the text to "Double Magic!".
68+
3. Create an input box and use the `keyup` event to display whatever the user types onto the screen.
69+
70+
## The Event Object (`e`)
71+
72+
When an event happens, JavaScript automatically sends a "Package" of information to your function. We usually call this package `e` or `event`.
73+
74+
It contains useful data, like which key was pressed or exactly where the mouse was clicked.
75+
76+
```javascript
77+
const myInput = document.querySelector('input');
78+
79+
myInput.addEventListener('keydown', function(e) {
80+
console.log(`You just pressed the ${e.key} key!`);
81+
});
82+
```
83+
84+
## The "Interactive" Formula
85+
86+
To build any interactive feature on **CodeHarborHub**, just follow this formula:
87+
88+
1. **Target:** Find the element (`querySelector`).
89+
2. **Listen:** Add the listener (`addEventListener`).
90+
3. **React:** Write the code to change the DOM inside the function.
91+
92+
## Summary Checklist
93+
94+
* [x] I know that an **Event** is a signal that something happened.
95+
* [x] I can use `addEventListener` to trigger code.
96+
* [x] I understand common events like `click`, `input`, and `mouseover`.
97+
* [x] I know how to use the event object (`e`) to get extra details.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
sidebar_position: 4
3+
title: "Functions: The Action Creators"
4+
sidebar_label: Functions
5+
description: "Learn how to write reusable blocks of code that perform specific tasks."
6+
---
7+
8+
A **Function** is a reusable block of code designed to perform a specific task. Instead of writing the same 10 lines of code over and over again, you wrap them in a function and "call" it whenever you need it.
9+
10+
## The "Cooking" Analogy
11+
12+
Think of a function like a **Microwave**.
13+
1. **The Input:** You put in a cold slice of pizza (Data).
14+
2. **The Process:** You press the "Start" button (The Function runs its code).
15+
3. **The Output:** You get back a hot slice of pizza (The Result).
16+
17+
You don't need to know how the microwave works inside every time; you just need to know how to press the button!
18+
19+
## How to Build a Function
20+
21+
To create a function, you follow these steps:
22+
23+
1. **Declare** it (Give it a name).
24+
2. **Define** the task (Write the code inside `{ }`).
25+
3. **Call** it (Tell it to run!).
26+
27+
```javascript
28+
// 1. Defining the function
29+
function sayHello() {
30+
console.log("Hello CodeHarborHub!");
31+
}
32+
33+
// 2. Calling (running) the function
34+
sayHello();
35+
```
36+
37+
## Inputs: Parameters & Arguments
38+
39+
Sometimes, a function needs extra information to do its job. We call these **Parameters**.
40+
41+
Imagine a greeting function. It’s boring if it only says "Hello." We want it to say hello to a *specific* person.
42+
43+
```javascript
44+
// 'name' is a parameter (a placeholder)
45+
function greetUser(name) {
46+
console.log(`Hello, ${name}! Welcome back.`);
47+
}
48+
49+
// 'Ajay' is the argument (the actual data)
50+
greetUser("Ajay");
51+
greetUser("Vikas");
52+
```
53+
54+
## Outputs: The `return` Keyword
55+
56+
Most functions don't just "do" something; they "give back" a result. We use the `return` keyword for this. Once a function hits `return`, it stops and hands the value back to you.
57+
58+
```javascript
59+
function addNumbers(a, b) {
60+
return a + b;
61+
}
62+
63+
let sum = addNumbers(5, 10); // sum now holds the value 15
64+
console.log(sum);
65+
```
66+
67+
## Interactive Lab: The "Magic Math Machine"
68+
69+
In this CodePen, try changing the numbers being passed into the function. Watch how the function processes the math and updates the screen!
70+
71+
<CodePenEmbed
72+
title="JS Functions Practice"
73+
penId="Kwgdgao"
74+
/>
75+
76+
### Challenge Tasks:
77+
78+
1. Look for the `multiply` function in the JS tab.
79+
2. Create a new function called `subtract` that takes two numbers and returns the difference.
80+
3. Call your new function and log the result to the console.
81+
82+
## Why Use Functions?
83+
84+
* **Don't Repeat Yourself (DRY):** Write once, use a thousand times.
85+
* **Organization:** Break a big, scary project into small, manageable "tasks."
86+
* **Easy Testing:** If something breaks, you know exactly which "recipe" is wrong.
87+
88+
## Summary Checklist
89+
90+
* [x] I know that a function is a **reusable** block of code.
91+
* [x] I understand that **Parameters** are inputs and **Return** is the output.
92+
* [x] I can define a function using the `function` keyword.
93+
* [x] I can "call" a function to make it run.

0 commit comments

Comments
 (0)