🌐 DOM Manipulation
Build Interactive Websites!
Combine HTML, CSS, and JavaScript to create awesome web experiences!
HTML + CSS + JavaScript = Interactive Web!
You've learned JavaScript - now let's connect it to web pages! Websites need all three technologies working together:
🏗️ The Web Technology Stack
- HTML: Structure and content (the skeleton)
- CSS: Styling and layout (the appearance)
- JavaScript: Interactivity and behavior (the brain)
Analogy: Think of a house - HTML is the structure, CSS is the paint and decoration, JavaScript is the electricity that makes things work!
Key Concepts
DOM
Document Object Model - JavaScript's way to access HTML elements.
Event Listeners
Detect user actions like clicks, keypresses, and mouse movements.
getElementById
Find specific HTML elements by their ID to manipulate them.
innerHTML
Change the content inside HTML elements dynamically.
style Property
Change CSS styles of elements using JavaScript.
User Input
Get data from text boxes and forms to use in programs.
Accessing HTML Elements
🎯 Finding Elements with JavaScript
HTML:
JavaScript:
Result: The heading text changes from "Hello!" to "Selamat datang!"
Event Listeners - Responding to Clicks
🖱️ Click Events
HTML:
JavaScript:
How it works: addEventListener waits for a click, then runs the function!
Interactive Demo - Try It!
🎮 Live Button Example
Changing Styles with JavaScript
🎨 Dynamic Styling
HTML:
JavaScript:
Getting User Input
📝 Text Input Example
HTML:
JavaScript:
Keyboard Events
⌨️ Detecting Key Presses
Common events: "keypress", "keydown", "keyup"
Project Example: Simple Counter
🔢 Click Counter
HTML:
JavaScript:
Malaysian Quiz Game Example
🇲🇾 Simple Quiz
Common Mistakes to Avoid
⚠️ Watch Out For:
- Wrong IDs: Make sure HTML id matches getElementById!
- Script timing: Put JavaScript after HTML or use DOMContentLoaded
- Forgetting .value: Use .value to get input text, not .innerHTML
- Missing quotes: Event names need quotes: "click" not click
Summary
You've learned:
- ✅ The DOM (Document Object Model) is how JavaScript talk to HTML
- ✅ getElementById() finds specific HTML elements
- ✅ querySelector() uses CSS rules to find elements
- ✅ .innerHTML and .textContent change what elements say
- ✅ .style changes how elements look (CSS)
- ✅ addEventListener() makes elements respond to clicks and typing
🎮 Interactive Challenge: Try These Live Demos!
Play with these interactive examples, then create your own:
📊 Live Demo: Click Counter
0
Try it! Click the buttons to see the counter change in real-time.
👋 Live Demo: Name Greeter
🎨 Live Demo: Background Color Changer
Click any color button to change this demo's background!
📝 Code Example: Name Greeter
Here's how the Name Greeter demo works:
🎨 Challenge 2: Color Changer
Create buttons that change the background color:
💰 Challenge 3: Simple Calculator
Build a basic addition calculator:
🎯 Project Activity: Malaysian Culture Quiz
What You'll Build:
Create an interactive quiz about Malaysian culture, food, and places!
Instructions:
- Create an HTML file with quiz questions
- Add buttons for answer choices
- Use JavaScript to check answers and keep score
- Display results with colors (green for correct, red for wrong)
- Add a "Next Question" button to move through questions
Example Quiz Question:
Quiz Topics to Include:
- Malaysian foods (Nasi Lemak, Roti Canai, etc.)
- Famous places (KLCC, Batu Caves, Penang, etc.)
- Malaysian culture and traditions
- Languages spoken in Malaysia
- Malaysian festivals (Hari Raya, CNY, Deepavali)
💪 Practice Challenges
Challenge 1: E-Wallet Calculator
Create a simple e-wallet balance tracker:
- Display current balance
- Button to add money
- Button to spend money
- Input field for amount
- Update balance display after each transaction
Hint: Start with balance = 100.00
Challenge 2: BMI Calculator
Build a Body Mass Index calculator:
- Input for weight (kg)
- Input for height (cm)
- Button to calculate BMI
- Display BMI result with category (Underweight, Normal, Overweight)
Formula: BMI = weight / (height in meters)²
Challenge 3: Random Number Game
Create a guessing game:
- Computer picks a random number 1-10
- User enters a guess
- Show "Too high", "Too low", or "Correct!"
- Keep track of number of attempts
Hint: Use Math.random() for random numbers
Challenge 4: To-Do List
Build a simple task list:
- Input field to enter tasks
- Button to add task to list
- Display all tasks
- Button to clear all tasks
Extra: Add ability to mark tasks as complete!
Challenge 5: Traffic Light Simulator
Create an interactive traffic light:
- Three colored circles (red, yellow, green)
- Buttons to change which light is on
- Only one light can be on at a time
- Change circle colors using style.backgroundColor
Challenge: Add automatic cycling every 3 seconds!