TGViewer
Web Development Web Development @webdevcoursefree · 79.7K subscribers
Post #3501 4.13K
Now, let's move to the the next topic:

🖱️ JavaScript Events

Events are actions performed by the user or the browser.

Examples
• Clicking a button
• Typing in input
• Submitting a form
• Pressing a key
• Page loading

JavaScript listens to these events and responds.

🧠 Why Events Matter

Without events
• No button clicks
• No form submission handling
• No real interactivity

Events = user + JavaScript talking to each other 🤝

🎧 Event Listener Concept

JavaScript listens for an event and runs a function.

Syntax: element.addEventListener("event", function);

🖱️ Click Event

HTML:
<button id="btn">Click Me</button>

JavaScript:
const btn = document.getElementById("btn");
btn.addEventListener("click", () => {
console.log("Button clicked");
});

Use cases:
• Toggle theme
• Open modal
• Submit action

⌨️ Input Event
Triggered when user types.

HTML:
<input type="text" id="name" />

JavaScript:

const input = document.getElementById("name");
input.addEventListener("input", () => {
console.log(input.value);
});

Use cases:
• Live validation
• Search suggestions

📤 Submit Event
Used for forms.
HTML:
<form id="myForm"> <input type="email" /> <button>Submit</button>
</form>

JavaScript:

const form = document.getElementById("myForm");
form.addEventListener("submit", (e) => {
e.preventDefault();
console.log("Form submitted");
});

⚠️ preventDefault() stops page reload.

⌨️ Keyboard Events
Common types:
• keydown
• keyup

Example:
document.addEventListener("keydown", (e) => {
console.log(e.key);
});

Use cases:
• Shortcuts
• Game controls

🧩 Event Object
Event object gives details.

Common properties:
• e.target
• e.type
• e.key
btn.addEventListener("click", (e) => {
console.log(e.target);
});

⚠️ Common Beginner Mistakes
• Forgetting preventDefault()
• Using inline HTML events
• Attaching event before DOM loads
• Using wrong event type

🧪 Mini Practice Task
• Add click event to change text
• Show live input value
• Prevent form submission reload
• Detect key press


✅ Mini Practice Task – Solution
🖱️ JavaScript Events

🟦 1️⃣ Add click event to change text

HTML
<h2 id="text">Hello</h2> <button id="btn">Change Text</button>

JavaScript

const text = document.getElementById("text");
const btn = document.getElementById("btn");
btn.addEventListener("click", () => {
text.textContent = "Text changed!";
});

✔️ Text updates on button click

🟩 2️⃣ Show live input value

HTML
<input type="text" id="inputBox" /> <p id="output"></p>

JavaScript

const input = document.getElementById("inputBox");
const output = document.getElementById("output");
input.addEventListener("input", () => {
output.textContent = input.value;
});

✔️ Text updates as user types

🟥 3️⃣ Prevent form submission reload

HTML
<form id="myForm"> <input type="email" required /> <button>Submit</button> </form>

JavaScript

const form = document.getElementById("myForm");
form.addEventListener("submit", (e) => {
e.preventDefault();
console.log("Form submitted without reload");
});

✔️ Page does not refresh

⌨️ 4️⃣ Detect key press

JavaScript

document.addEventListener("keydown", (e) => {
console.log("Key pressed:", e.key);
});

✔️ Logs pressed key in console

🧠 What you learned
• Handling click events
• Real-time input tracking
• Form control
• Keyboard event handling


➡️ Double Tap ♥️ For More
  • ❤ 15
More from @webdevcoursefree
  1. Oct 14, 2026🌐 Web Development Roadmap — Part 17 HTML Images — <img> Tag Images make webpages more vis…
  2. Oct 13, 2026𝗧𝗢𝗣 𝗙𝗥𝗘𝗘 𝗖𝗢𝗨𝗥𝗦𝗘𝗦 𝗧𝗢 𝗕𝗢𝗢𝗦𝗧 𝗬𝗢𝗨𝗥 𝗦𝗞𝗜𝗟𝗟𝗦 𝗜𝗡 𝟮𝟬𝟮𝟲! Explor…
  3. Oct 12, 2026For example: Go to Profile → <a> Submit Form → <button> Open Article → <a> Delete Account…
  4. Oct 12, 2026🌐 Web Development Roadmap — Part 16 HTML Links — Tag Links are one of the most important…
  5. Oct 12, 2026🚀 𝗪𝗵𝗶𝗰𝗵 𝗧𝗲𝗰𝗵 𝗦𝗸𝗶𝗹𝗹 𝗪𝗶𝗹𝗹 𝗬𝗼𝘂 𝗠𝗮𝘀𝘁𝗲𝗿 𝗶𝗻 𝟮𝟬𝟮𝟲? Want to buil…
  6. Oct 9, 2026Post #4079
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →