Featured
- Get link
- X
- Other Apps
🧩 Day 41 — DOM Event Delegation & Form Validation
🧩 Day 41 — DOM Event Delegation & Form Validation
Hi Guys Welcome to Day 41 🧠 Today, we’ll master two vital concepts for any modern JavaScript developer: Event Delegation and Form Validation. These two make your sites faster, cleaner, and more user-friendly — and they’re essential for interviews too.
⚙️ 1️⃣ What is Event Delegation?
Normally, you attach an event listener to every button or element individually — but that can slow down your app when you have many items. Event Delegation lets you listen for all those events at once on a common ancestor, and handle them dynamically.
<ul id="todoList">
<li>Learn HTML</li>
<li>Practice CSS</li>
<li>Master JavaScript</li>
</ul>
<script>
const list = document.getElementById("todoList");
list.addEventListener("click", (e) => {
if (e.target.tagName === "LI") {
e.target.style.textDecoration = "line-through";
}
});
</script>
✅ No matter how many list items you add later, they’ll all work automatically! This saves memory and keeps your JS optimized.
🧱 2️⃣ Adding New Elements Dynamically
const newItem = document.createElement("li");
newItem.textContent = "Explore React";
list.appendChild(newItem); // still works without new event listener
🧠 3️⃣ Event Bubbling vs Capturing
When an event happens, it travels from the element up to its ancestors — that’s called event bubbling. Delegation works because JS lets you catch that bubbling event at a parent node.
📋 4️⃣ Form Validation Basics
Form validation ensures users enter correct data before submission. Let’s build a simple signup form and validate it using JavaScript.
<form id="signupForm">
<label>Name:</label>
<input type="text" id="name" required><br>
<label>Email:</label>
<input type="email" id="email" required><br>
<label>Password:</label>
<input type="password" id="password" required><br>
<button type="submit">Register</button>
</form>
<script>
document.getElementById("signupForm").addEventListener("submit", function(e){
e.preventDefault();
const name = document.getElementById("name").value.trim();
const email = document.getElementById("email").value.trim();
const password = document.getElementById("password").value;
if(name.length < 3){
alert("Name must be at least 3 characters long.");
return;
}
if(!email.includes("@")){
alert("Enter a valid email address.");
return;
}
if(password.length < 6){
alert("Password must be at least 6 characters.");
return;
}
alert("Registration successful 🎉");
});
</script>
💡 5️⃣ Improved UX with Inline Validation
You can validate each field on blur or keypress to give immediate feedback instead of alerts.
document.getElementById("email").addEventListener("input", (e)=>{
const hint = document.getElementById("emailHint");
if(!e.target.value.includes("@")){
hint.textContent = "❌ Invalid email";
} else {
hint.textContent = "✅ Looks good!";
}
});
🧩 6️⃣ Best Practices
- Use HTML5 validation attributes like
required,pattern. - Always sanitize input on the server too (for security).
- Show inline messages instead of alerts for a better user experience.
- Use event delegation for dynamic forms (e.g., adding multiple inputs).
🎯 Summary
You’ve learned how to handle user interactions smartly using Event Delegation and validated forms like a pro. Tomorrow, we’ll enhance UI components using Bootstrap + JavaScript for modals, alerts, and interactive elements.
Popular Posts
💙 Day 27: Bootstrap Mini Project – Responsive Portfolio Page
- Get link
- X
- Other Apps
🎨 Day 2: Headings, Paragraphs & Text Formatting
- Get link
- X
- Other Apps
Comments
Post a Comment