Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

JavaScript Events and Event Handling

Lesson 23 of 34 in the free Web Technologies notes on Siksha Sarovar, written by Rohit Jangra.

JavaScript Events and Event Handling

What are Events?

Events are actions or occurrences that happen in the browser — user clicks, key presses, page load, form submissions, etc. JavaScript can respond to these events through event handlers.

Ways to Handle Events

// 1. HTML attribute (inline — not recommended)
// <button onclick="handleClick()">Click</button>

// 2. DOM property
const btn = document.getElementById("myBtn");
btn.onclick = function() { alert("Clicked!"); };

// 3. addEventListener (preferred — supports multiple handlers)
btn.addEventListener("click", function(event) {
    console.log("Button clicked!");
    console.log(event.target); // The clicked element
});

// Arrow function style
btn.addEventListener("click", (e) => {
    e.preventDefault(); // prevent default behavior
    console.log("Arrow handler");
});

// Remove listener
const handler = () => console.log("removed");
btn.addEventListener("click", handler);
btn.removeEventListener("click", handler);

Common Event Types

CategoryEvents
Mouseclick, dblclick, mouseover, mouseout, mousemove, mousedown, mouseup
Keyboardkeydown, keyup, keypress
Formsubmit, change, input, focus, blur, reset
Windowload, DOMContentLoaded, resize, scroll, unload
Touchtouchstart, touchend, touchmove
Dragdragstart, dragend, drop, dragover

Event Object

document.addEventListener("keydown", (e) => {
    console.log(e.key);        // "Enter", "a", etc.
    console.log(e.keyCode);    // 13, 65, etc.
    console.log(e.ctrlKey);    // true if Ctrl held
    console.log(e.shiftKey);   // true if Shift held
});

document.addEventListener("click", (e) => {
    console.log(e.clientX, e.clientY);  // mouse position
    console.log(e.target);              // element clicked
    console.log(e.currentTarget);       // element with listener
});

Event Propagation

// Bubbling: event travels from child UP to parent (default)
// Capturing: event travels from parent DOWN to child

document.getElementById("parent").addEventListener("click", () => {
    console.log("Parent clicked");
}, false); // false = bubbling phase (default)

document.getElementById("child").addEventListener("click", (e) => {
    console.log("Child clicked");
    e.stopPropagation(); // stop bubbling to parent
});

Event Delegation

// Instead of adding listener to each button:
document.getElementById("list").addEventListener("click", (e) => {
    if (e.target.tagName === "LI") {
        e.target.classList.toggle("selected");
    }
});
// Works even for dynamically added items!

Form Event Example

const form = document.getElementById("loginForm");
form.addEventListener("submit", (e) => {
    e.preventDefault();     // stop form from reloading page
    const email = document.getElementById("email").value;
    const pass = document.getElementById("password").value;
    if (email && pass) {
        console.log("Logging in:", email);
    }
});
Key Takeaway: Always use addEventListener for event handling. Understand event bubbling and use e.preventDefault() to stop default browser actions. Event delegation improves performance for lists and dynamic content.