π JavaScript Event Handling

Full Stack Developer Intern at DevSync
Presented by Swapnil Meshram within mentorship Devsync
π Started learning JavaScript with the guidance of Devsync, focusing on concepts of events and event handling with simple hands-on practice.
π 1. What is an event in JavaScript?
An event is an action that happens in the browser, triggered by the user or the browser itself.
πΉ Examples:
A user clicks a button
A user types in a form
A page loads
A mouse moves, or a key is pressed
π 2. What is event handling?
Event handling means writing JavaScript code that listens for these events and responds when they happen.
π Example: Show an alert when a button is clicked.
π 3. Ways to Handle Events in JavaScript
There are three main ways to attach event handlers:
β Method 1: HTML Event Attributes (Inline)
<button onclick="greet()">Click Me</button>
<script>
function greet() {
alert("Hello from inline event!");
}
</script>
πΉ Simple but not recommended for larger projects. Keeps logic mixed with HTML.
β Method 2: Using DOM Element Properties
<button id="btn">Click</button>
<script>
let button = document.getElementById("btn");
button.onclick = function () {
alert("Button clicked!");
};
</script>
πΉ Better than inline, but still only allows one handler per event.
β
Method 3: Using addEventListener()
<button id="btn">Click</button>
<script>
let button = document.getElementById("btn");
button.addEventListener("click", function () {
alert("Handled using addEventListener!");
});
</script>
πΉ This is the recommended way:
Keeps JavaScript and HTML separate
Allows multiple listeners
Works with all event types
π 4. Common Event Types
| Event Type | Description |
click | When user clicks an element |
mouseover | Mouse hovers over an element |
mouseout | Mouse leaves an element |
keydown | The key is pressed down |
keyup | Key is released |
input | User types in input field |
submit | The form is submitted |
change | The value of an input changes |
load | Page or image finishes loading |
π 5. Event Object
Every event returns an event object that contains useful information such as
element.addEventListener("click", function (event) {
console.log(event.type); // "click"
console.log(event.target); // element that was clicked
});
β
The eventobjectis automatically passed to the event handler.
π 6. Handling Input Events
πΉβ£input Event (fires as the user types)
<input id="name" type="text">
<script>
document.getElementById("name").addEventListener("input", function (e) {
console.log("Typed: " + e.target.value);
});
</script>
π 7. Handling Form Submit Event
Prevent the form from refreshing the page using event.preventDefault().
<form id="myForm">
<input type="text" />
<button type="submit">Submit</button>
</form>
<script>
document.getElementById("myForm").addEventListener("submit", function (e) {
e.preventDefault(); // stops page reload
alert("Form submitted!");
});
</script>
π 8. Removing Event Listeners
You can remove an event listener uusingremoveEventListener()
function sayHello() {
alert("Hello!");
}
let btn = document.getElementById("btn");
btn.addEventListener("click", sayHello);
// To remove it:
btn.removeEventListener("click", sayHello);
π« Note: You must pass the same function reference when removing.
π 9. Event Delegation (Advanced Concept)
Instead of attaching events to multiple child elements, you can attach a single event listener to a parent and use iitevent.targetto identify what was clicked.
<ul id="myList">
<li>Item 1</li>
<li>Item 2</li>
</ul>
<script>
document.getElementById("myList").addEventListener("click", function (e) {
if (e.target.tagName === "LI") {
alert("Clicked on: " + e.target.textContent);
}
});
</script>
β Useful for dynamic elements created later with JavaScript.
π 10. Summary Table
| Task | Method Example |
| Attach event (best method) | addEventListener("click", function) |
| Access event info | function(e) { e.target, e.type } |
| Prevent form reload | event.preventDefault() |
| Detect input typing | addEventListener("input", function) |
| Detect form submission | addEventListener("submit", function) |
| Remove event | removeEventListener("click", handlerFunction) |
π Mini Practice Project: Toggle Text
<button id="toggleBtn">Show/Hide</button>
<p id="text" style="display: none;">Hello JavaScript Events!</p>
<script>
let button = document.getElementById("toggleBtn");
let text = document.getElementById("text");
button.addEventListener("click", function () {
if (text.style.display === "none") {
text.style.display = "block";
} else {
text.style.display = "none";
}
});
</script>
β Final Tips
Use
addEventListener()for all modern JS event handling.Always pass a named function when you need to remove it later.
Use
event.targetto identify the exact element interacted with.For forms, always use
preventDefault()to avoid refresh.