JavaScript Foundations • StreetGeek Academy
Learn how to detect and respond to user actions (clicks, keypresses, scrolls, etc.) and make your site dynamic, responsive, and engaging.
Events are actions that happen in the browser — and JavaScript can “listen” for them and respond.
Common examples:
When an event occurs, you can run code in response.
addEventListener() MethodAttach an event listener to any element:
element.addEventListener("event", callbackFunction);const btn = document.getElementById("clickMe");
btn.addEventListener("click", () => {
console.log("Button clicked!");
});| Event | Trigger | Example Use |
|---|---|---|
click | When clicked | Buttons, links |
mouseover / mouseout | Hover enter/leave | Animations |
input | While typing | Live form feedback |
submit | Form submission | Validation |
keydown / keyup | Keyboard actions | Hotkeys, search |
scroll | Page scroll | Lazy loading, animations |
change | Value change | Dropdowns, checkboxes |
addEventListener works with all of these and more.<button id="menu-btn">☰ Menu</button>
<nav id="nav" class="hidden">
<a href="#">Home</a>
<a href="#">Projects</a>
<a href="#">Contact</a>
</nav>.hidden { display: none; }const menuBtn = document.getElementById("menu-btn");
const nav = document.getElementById("nav");
menuBtn.addEventListener("click", () => {
nav.classList.toggle("hidden");
});Each event automatically sends an event object with useful info.
document.addEventListener("click", (event) => {
console.log("Clicked element:", event.target);
});| Property | Description |
|---|---|
event.target | The element that triggered the event. |
event.type | Type of event (click, scroll, etc.). |
event.preventDefault() | Stops default behavior (like form submission). |
<button id="like-btn">❤️ Like</button>
<p id="likes">Likes: 0</p>const likeBtn = document.getElementById("like-btn");
const likesDisplay = document.getElementById("likes");
let likes = 0;
likeBtn.addEventListener("click", () => {
likes++;
likesDisplay.textContent = `Likes: ${likes}`;
});<input type="text" id="nameInput" placeholder="Enter your name" />
<p id="preview"></p>const nameInput = document.getElementById("nameInput");
const preview = document.getElementById("preview");
nameInput.addEventListener("input", () => {
preview.textContent = `Hello, ${nameInput.value || "friend"}!`;
});<form id="contactForm">
<input type="email" id="email" placeholder="Your email" required />
<button type="submit">Send</button>
</form>
<p id="status"></p>const form = document.getElementById("contactForm");
const status = document.getElementById("status");
form.addEventListener("submit", (e) => {
e.preventDefault();
const email = document.getElementById("email").value;
if (email.includes("@")) {
status.textContent = "✅ Message sent successfully!";
} else {
status.textContent = "⚠️ Please enter a valid email.";
}
});document.addEventListener("keydown", (e) => {
console.log(`You pressed: ${e.key}`);
});window.addEventListener("scroll", () => {
console.log("Scrolling at", window.scrollY);
});Instead of adding listeners to every child element, attach one to the parent.
<ul id="project-list">
<li>Portfolio</li>
<li>ShopBlocks</li>
<li>Nfinite Dashboard</li>
</ul>const list = document.getElementById("project-list");
list.addEventListener("click", (e) => {
if (e.target.tagName === "LI") {
console.log(`Clicked project: ${e.target.textContent}`);
}
});1) What method attaches an event to an element?
2) What property tells you which element was clicked?
3) How do you prevent a form from reloading the page on submit?
4) What’s the difference between click and input events?
5) Why use event delegation?
Choose one (or all):
<button id="theme-toggle">🌙 Dark Mode</button>const toggle = document.getElementById("theme-toggle");
toggle.addEventListener("click", () => {
document.body.classList.toggle("dark");
toggle.textContent = document.body.classList.contains("dark")
? "☀️ Light Mode"
: "🌙 Dark Mode";
});<div class="accordion">
<button class="accordion-header">My Skills</button>
<div class="accordion-content">HTML, CSS, JS, PHP</div>
</div>const header = document.querySelector(".accordion-header");
header.addEventListener("click", () => {
header.nextElementSibling.classList.toggle("active");
});<textarea id="message" maxlength="150"></textarea>
<p id="counter">150 characters remaining</p>const textarea = document.getElementById("message");
const counter = document.getElementById("counter");
textarea.addEventListener("input", () => {
const remaining = 150 - textarea.value.length;
counter.textContent = `${remaining} characters remaining`;
});Next up → MODULE 8: Forms & Validation
We’ll take your interactivity further by validating user input, displaying error messages, and improving UX — all within your live portfolio site.