DEV Community

Cover image for Events & Event Handling in JavaScript – A Complete Beginner Guide
Vyankatesh Bairagi
Vyankatesh Bairagi

Posted on

Events & Event Handling in JavaScript – A Complete Beginner Guide

πŸ“Œ What is an Event in JavaScript?

An event is an action or occurrence that happens in the browser and can be detected by JavaScript.

Events allow JavaScript to respond to user interactions and browser activities, making web pages interactive and dynamic.

πŸ”Ή Events can be triggered by:

  • πŸ‘€ User actions (clicking, typing, hovering)
  • 🌐 Browser actions (page load, resize, scroll)

πŸ”Ή Common Examples of Events:

  • Clicking a button
  • Pressing a keyboard key
  • Hovering over an element
  • Loading a web page

πŸ‘‰ In simple words:

Event = Something happens β†’ JavaScript reacts


πŸ“Œ Types of Events in JavaScript

JavaScript provides many built-in events, grouped by their purpose.


πŸ–± Mouse Events

Triggered by mouse interactions.

  • click – when an element is clicked
  • dblclick – when double-clicked
  • mouseover – when mouse enters an element
  • mouseout – when mouse leaves an element

⌨ Keyboard Events

Triggered by keyboard actions.

  • keydown – when a key is pressed
  • keyup – when a key is released

πŸ“ Form Events

Triggered by form-related actions.

  • submit – when a form is submitted
  • change – when input value changes
  • focus – when input gets focus
  • blur – when input loses focus

🌐 Browser / Window Events

Triggered by browser-level activities.

  • load – when the page finishes loading
  • resize – when the window size changes
  • scroll – when the page is scrolled

πŸ“Œ What is Event Handling?

Event handling is the process of writing JavaScript code that runs when an event occurs.

πŸ“Œ It defines how your application reacts to user actions.

πŸ”Ή Example Explanation:

When a user clicks a button β†’

JavaScript executes a function β†’

An action is performed (show message, submit form, etc.)

Event β†’ Handler β†’ Action


πŸ“Œ Ways to Handle Events in JavaScript

There are three main ways to handle events in JavaScript.


❌ 1. Inline Event Handling (Not Recommended)

<button onclick="alert('Clicked')">Click</button>
Enter fullscreen mode Exit fullscreen mode

⚠ Why this is NOT recommended:
Mixes HTML and JavaScript
Hard to maintain in large projects
Not scalable
Poor separation of concerns

πŸ“Œ This method is outdated and should be avoided in modern development.

2. DOM Property Method

const button = document.querySelector("button");
button.onclick = function () {
  console.log("Button clicked");
};
Enter fullscreen mode Exit fullscreen mode

❌ Drawback:

Only one event handler can exist
New handler overrides the previous one

3. Best Method: addEventListener (Recommended)

const button = document.querySelector("button");

button.addEventListener("click", () => {
  console.log("Button clicked");
});
Enter fullscreen mode Exit fullscreen mode

βœ… Why addEventListener is best:

Allows multiple event handlers

Cleaner and scalable code

Modern JavaScript standard

Works well with frameworks like React

πŸ’‘ Best Practice:

Always use addEventListener for handling events in real-world applications.

Top comments (0)