Let’s begin with Lecture 30 of the HTML5 & CSS3 series. Adding Simple JavaScript β Make Your Website Interactive. This lecture is for absolute beginners; even kids can follow along and understand.
π― Objective:
By the end of this lecture, you will:
- Understand what JavaScript is and why it’s important
- Know how to add JavaScript to your HTML page
- Be able to respond to user actions like clicks and hovers
- Add simple interactivity like alerts , text changes , and dark mode toggle
- Bring your HTML & CSS website to life with real interaction!
π‘ What Is JavaScript?
JavaScript is a programming language that makes websites interactive.
Think of it like this:
If HTML is the skeleton and CSS is the skin , then JavaScript is the brain β it helps your site think, react, and do things!
With JavaScript, you can:
- Show messages when someone clicks a button
- Change content without reloading the page
- Toggle dark/light mode
- Validate forms
- Animate elements
- And much more!
π§ How to Add JavaScript to Your Page
There are two ways:
1. Inline Script (inside HTML)
<script>
alert("Welcome to my website!");
</script>
Place this before </body>
for better performance.
2. External Script (best practice)
Create a file named script.js
and link it like this:
<script src="script.js"></script>
This keeps your code organized and reusable.
π§ Basic JavaScript Concepts
CONCEPT | DESCRIPTION |
---|---|
Variables | Store values like numbers or text |
Functions | Reusable blocks of code |
Events | Things that happen in the browser (clicks, hovers, etc.) |
DOM Manipulation | Changing HTML/CSS using JavaScript |
π» Try This: Add a Welcome Alert
Letβs make your portfolio page show a friendly greeting when it loads.
Step 1: Open portfolio.html
Go to your personal portfolio page from Lecture 29.
Build a Personal Portfolio Page | Lecture 29
Step 2: Add This Code Before </body>
<script>
// Show welcome message
alert("Welcome to My Portfolio!");
</script>
Now every time someone visits your page, theyβll see a greeting! π
π‘ Bonus: Add a Button That Changes Text
Letβs create a button that updates a paragraph when clicked.
Step 1: Add HTML
Add this inside the <main>
section of your portfolio page:
<section id="interactive">
<h2>Interactive Section</h2>
<p id="message">Click the button to change this text!</p>
<button onclick="changeText()">Change Text</button>
</section>
Step 2: Add JavaScript
Update your <script>
block or script.js
with:
<script>
function changeText() {
document.getElementById("message").innerHTML = "You changed the text! π";
}
</script>
π Now clicking the button changes the message dynamically!

π‘ Bonus: Add a Dark Mode Toggle
Letβs let users switch between light and dark themes using JavaScript.
Step 1: Define a Dark Theme in CSS
Add this inside <style>
:
.dark-mode {
background-color: #1e1e1e;
color: #f1f1f1;
}
.dark-mode .card, .dark-mode header, .dark-mode footer {
background-color: #2d2d2d;
}
Step 2: Add Toggle Button in HTML
Inside your <header>
or <footer>
:
<button onclick="toggleDarkMode()">Toggle Dark Mode</button>
Step 3: Add JavaScript Function
Update your <script>
block with:
<script>
function toggleDarkMode() {
document.body.classList.toggle("dark-mode");
}
</script>
Now users can toggle between light and dark mode with one click! ππ‘
π§ͺ Try It Yourself!
- Add a counter button that increases a number each time it’s clicked.
- Create a “Show More” button under the About section to reveal hidden text.
- Add an
onmouseover
effect that changes heading color when hovered. - Style the dark mode further by changing all links and buttons to look good in dark theme.
π Summary of What You Learned Today
β You now know how to:
- Add JavaScript to your HTML page
- Use functions to run code on events like
onclick
- Modify HTML content using
document.getElementById()
- Toggle styles and modes using JavaScript
- Make your site interactive and fun!
π Next Lecture Preview:
In Lecture 31 , weβll explore more JavaScript basics β including variables, data types, and conditionals β so you can write smarter, more powerful scripts for your webpages!
Stay Updated
If you found this information useful, donβt forget to bookmark this page and Share.
HTML5 and CSS3 Compleate Series