Let’s begin with Lecture 35 of the HTML5 & CSS3 series. Advanced DOM Manipulation β Classes, Themes & Keyboard Events. This lecture is for absolute beginners; even kids can follow along and understand.
π― Objective:
By the end of this lecture, you will:
- Know how to add, remove, and toggle classes using JavaScript
- Be able to build a dark mode toggle using class switching
- Learn how to animate elements by modifying styles dynamically
- Understand how to respond to keyboard events
- Make your website more interactive and dynamic using advanced DOM techniques!
π§ What Youβll Learn Today
SKILL | DESCRIPTION |
---|---|
classList | Add, remove, or toggle CSS classes with JavaScript |
addEventListener | Listen for user actions like keypress or input |
key | Detect which key was pressed |
Dynamic Styling | Change styles on the fly without hardcoding |
Real-Time Updates | React instantly to user input and actions |
π¨ Using classList
to Control Styles
The classList
property lets you easily work with classes β perfect for toggling themes or effects.
Methods:
METHODS | PURPOSE |
---|---|
element.classList.add("class") | Adds a class |
element.classList.remove("class") | Removes a class |
element.classList.toggle("class") | Toggles (adds/removes) a class |
element.classList.contains("class") | Checks if element has a class |
Example:
let box = document.getElementById("myBox");
box.classList.add("highlight"); // Add highlight style
box.classList.remove("highlight"); // Remove highlight
box.classList.toggle("highlight"); // Toggle it on/off
π Build a Dark Mode Toggle with Class Switching
Letβs improve our dark mode feature from earlier by using classList
.
Step 1: Update Your CSS
Add a .dark-mode
class:
.dark-mode {
background-color: #1e1e1e;
color: white;
}
Also update nested elements if needed:
.dark-mode .card, .dark-mode header, .dark-mode footer {
background-color: #2d2d2d;
}
Step 2: Add a Button
<button onclick="toggleDarkMode()">Toggle Dark Mode</button>
Step 3: Add JavaScript Function
<script>
function toggleDarkMode() {
document.body.classList.toggle("dark-mode");
}
</script>
π Now users can switch between light and dark theme with one click!
π±οΈ Respond to Keyboard Events
You can make your site respond to keyboard keys β like pressing Enter to submit a form or Spacebar to play music.
Common Key Events:
keydown
β when a key is pressed downkeyup
β when a key is released
Example: Show Key Pressed
<p>Press any key to see its name!</p>
<p id="keyOutput"></p>
<script>
document.addEventListener("keydown", function(event) {
document.getElementById("keyOutput").textContent = "You pressed: " + event.key;
});
</script>
Now as you press keys, their names appear instantly!

π‘ Bonus: Create a Flashlight Effect with Mouse Movement
Letβs create a fun effect where only the area under the mouse is visible.
Step 1: Add HTML
<h2>Flashlight Mode</h2>
<div id="flashlight" style="position: fixed; width: 200px; height: 200px; border-radius: 50%; pointer-events: none; mix-blend-mode: difference; background: white;"></div>
Step 2: Add JavaScript
<script>
let flashlight = document.getElementById("flashlight");
document.addEventListener("mousemove", function(e) {
flashlight.style.left = e.pageX - 100 + "px";
flashlight.style.top = e.pageY - 100 + "px";
});
</script>
And add this CSS:
body {
transition: background-color 0.3s ease;
}
.dark-mode {
background-color: black;
}
Now move your mouse around β it’s like a real flashlight! π¦
π§ͺ Try It Yourself!
- Add a counter badge that shows how many items are in your list.
- Use
classList.contains()
to check if dark mode is active before changing text colors. - Animate an element across the screen using
setInterval()
andstyle.left
- Make the flashlight follow the mouse smoothly with
transition
andtransform
β Summary of What You Learned Today
CONCEPT | CODE EXAMPLE |
---|---|
Add Class | element.classList.add("highlight") |
Remove Class | element.classList.remove("highlight") |
Toggle Class | element.classList.toggle("dark-mode") |
Check Class | element.classList.contains("active") |
Keyboard Event | document.addEventListener("keydown", function(e) { ... }) |
Mouse Move | document.addEventListener("mousemove", function(e) { ... }) |
Dynamic Styling | element.style.left = e.pageX + "px" |
π Next Lecture Preview:
In Lecture 36 , weβll explore JavaScript objects and forms β so you can store complex data like user profiles, manage form submissions, and build interactive apps like login systems and contact forms!
Stay Updated
If you found this information useful, donβt forget to bookmark this page and Share.
HTML5 and CSS3 Compleate Series