{"id":43056,"date":"2024-03-02T18:04:33","date_gmt":"2024-03-02T12:34:33","guid":{"rendered":"https:\/\/www.guvi.in\/blog\/?p=43056"},"modified":"2026-09-09T04:07:17","modified_gmt":"2026-09-08T22:37:17","slug":"guide-for-events-in-javascript","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/guide-for-events-in-javascript\/","title":{"rendered":"JavaScript Events Complete Guide: addEventListener, Bubbling &#038; More (2026)"},"content":{"rendered":"\n<p>Every interactive action on a website starts with an event. A click can open a menu. A keypress can trigger a search. A form submission can send user data. Even moving the mouse over an element can change what appears on the screen.<\/p>\n\n\n\n<p>In JavaScript, events make these interactions possible by allowing the browser to detect user or system actions and run the appropriate code in response.<\/p>\n\n\n\n<p>From form validation and dropdown menus to keyboard shortcuts and dynamic content updates, JavaScript events are fundamental to modern web development. Understanding how they work is essential for building responsive and interactive applications.<\/p>\n\n\n\n<p>This guide explains events in JavaScript step by step, including common event types, <code>addEventListener()<\/code>, event bubbling and capturing, event delegation, and asynchronous event handling.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Quick Answer:<\/strong> <\/h2>\n\n\n\n<p>JavaScript events are actions such as clicks, keypresses, form submissions or mouse movements that trigger specific code in a web page.<\/p>\n\n\n\n<ul>\n<li><code>addEventListener()<\/code> attaches event handlers to elements.<\/li>\n\n\n\n<li>Common events include <code>click<\/code>, <code>submit<\/code>, <code>keydown<\/code> and <code>mouseover<\/code>.<\/li>\n\n\n\n<li>Event bubbling moves from child to parent elements.<\/li>\n\n\n\n<li>Event capturing moves from parent to child elements.<\/li>\n\n\n\n<li>Event delegation handles multiple child elements with one parent listener.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What Is a JavaScript Event?<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"630\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/09\/image-339-1200x630.png\" alt=\"\" class=\"wp-image-138030\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/09\/image-339-1200x630.png 1200w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/09\/image-339-300x158.png 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/09\/image-339-768x403.png 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/09\/image-339-1536x806.png 1536w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/09\/image-339-2048x1075.png 2048w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/09\/image-339-150x79.png 150w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/figure>\n\n\n\n<p>A JavaScript event is an action or occurrence detected by the browser that can trigger a specific piece of code.<\/p>\n\n\n\n<p>Events can be caused by user interactions such as:<\/p>\n\n\n\n<ul>\n<li>Clicking a button<\/li>\n\n\n\n<li>Pressing a key<\/li>\n\n\n\n<li>Submitting a form<\/li>\n\n\n\n<li>Moving the mouse<\/li>\n\n\n\n<li>Focusing on an input field<\/li>\n<\/ul>\n\n\n\n<p>They can also be triggered by the browser itself, such as when a page finishes loading.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>User clicks a button\n\u2192 Browser detects the click event\n\u2192 Event listener runs\n\u2192 JavaScript executes the required action\n<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Learn\/JavaScript\/Building_blocks\/Events\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Events in JavaScript<\/a> are handled through event handlers or event listeners. These are functions that tell JavaScript what should happen when a specific event occurs.<\/p>\n\n\n\n<p>In simple terms:<\/p>\n\n\n\n<p><strong>Event \u2192 Action occurs<\/strong><\/p>\n\n\n\n<p><strong>Event listener \u2192 Detects the action<\/strong><\/p>\n\n\n\n<p><strong>Event handler \u2192 Runs the required code<\/strong><\/p>\n\n\n\n<p>This event-driven behaviour is what makes websites respond dynamically instead of behaving like static pages.<\/p>\n\n\n\n<p><strong>Also, Find Out the<\/strong> <a href=\"https:\/\/www.guvi.in\/blog\/javascript-roadmap-for-beginners\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Best JavaScript Roadmap Beginners Should Follow<\/strong><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How Do JavaScript Events Work?<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"630\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/09\/image-340-1200x630.png\" alt=\"\" class=\"wp-image-138031\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/09\/image-340-1200x630.png 1200w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/09\/image-340-300x158.png 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/09\/image-340-768x403.png 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/09\/image-340-1536x806.png 1536w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/09\/image-340-2048x1075.png 2048w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/09\/image-340-150x79.png 150w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/figure>\n\n\n\n<p>Here\u2019s a simplified breakdown of how events work in JavaScript:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. Event Triggering<\/strong><\/h3>\n\n\n\n<p>An event occurs when a user or the browser performs an action.<\/p>\n\n\n\n<p>Examples include:<\/p>\n\n\n\n<ul>\n<li>Clicking a button<\/li>\n\n\n\n<li>Typing in an input field<\/li>\n\n\n\n<li>Hovering over an element<\/li>\n\n\n\n<li>Submitting a form<\/li>\n\n\n\n<li>Loading a page<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Event Listening<\/strong><\/h3>\n\n\n\n<p>JavaScript needs to know which event it should watch for.<\/p>\n\n\n\n<p>This is usually done with <code>addEventListener()<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>button.addEventListener(\"click\", handleClick);\n<\/code><\/pre>\n\n\n\n<p>Here, JavaScript listens for a <code>click<\/code> event on the button.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Event Handling<\/strong><\/h3>\n\n\n\n<p>When the event occurs, JavaScript runs the associated event handler function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function handleClick() {\n  console.log(\"Button clicked\");\n}\n<\/code><\/pre>\n\n\n\n<p>The handler contains the code that should execute in response to the event.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. Event Execution<\/strong><\/h3>\n\n\n\n<p>Once the event is detected, the browser executes the handler and updates the page or performs the required action.<\/p>\n\n\n\n<p>The complete flow looks like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>User action\n\u2192 Event triggered\n\u2192 Event listener detects it\n\u2192 Event handler runs\n\u2192 Page responds\n<\/code><\/pre>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>User clicks \"Submit\"\n\u2192 submit event fires\n\u2192 listener detects the event\n\u2192 validation function runs\n\u2192 form is submitted or an error is shown\n<\/code><\/pre>\n\n\n\n<p>Understanding this event flow is essential for building interactive web applications because it allows JavaScript to respond dynamically to user actions and browser activity.<\/p>\n\n\n\n<p><strong>Also Read: <a href=\"https:\/\/www.guvi.in\/blog\/javascript-questions-towards-better-interviews\/\" data-type=\"link\" data-id=\"https:\/\/www.guvi.in\/blog\/javascript-questions-towards-better-interviews\/\" target=\"_blank\" rel=\"noreferrer noopener\">42 JavaScript Questions Towards Better Interviews<\/a><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How to Create an Event in JavaScript?<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1200\" height=\"630\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/09\/image-341-1200x630.png\" alt=\"\" class=\"wp-image-138032\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/09\/image-341-1200x630.png 1200w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/09\/image-341-300x158.png 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/09\/image-341-768x403.png 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/09\/image-341-1536x806.png 1536w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/09\/image-341-2048x1075.png 2048w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/09\/image-341-150x79.png 150w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/figure>\n\n\n\n<p><a href=\"https:\/\/www.guvi.in\/blog\/javascript-tools-every-developer-should-know\/\" target=\"_blank\" rel=\"noreferrer noopener\">JavaScript<\/a> can respond to built-in browser events such as <code>click<\/code> and <code>keydown<\/code>, but developers can also create and dispatch their own custom events.<\/p>\n\n\n\n<p>The modern approach uses the <code>Event()<\/code> or <code>CustomEvent()<\/code> constructor along with <code>dispatchEvent()<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 1: Create the Event<\/strong><\/h3>\n\n\n\n<p>Create a new event object and give it a descriptive event type.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const customEvent = new Event(\"customEvent\", {\n  bubbles: true,\n  cancelable: true\n});\n<\/code><\/pre>\n\n\n\n<p>Here:<\/p>\n\n\n\n<ul>\n<li><code>customEvent<\/code> is the event type.<\/li>\n\n\n\n<li><code>bubbles: true<\/code> allows the event to move upward through the DOM.<\/li>\n\n\n\n<li><code>cancelable: true<\/code> allows its default behaviour to be cancelled when applicable.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 2: Select the Target Element<\/strong><\/h3>\n\n\n\n<p>Choose the element where the event should originate.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const targetElement = document.getElementById(\"targetElement\");\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 3: Dispatch the Event<\/strong><\/h3>\n\n\n\n<p>Use <code>dispatchEvent()<\/code> to trigger the custom event.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>targetElement.dispatchEvent(customEvent);\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Complete Example<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;button id=\"targetElement\"&gt;Trigger Event&lt;\/button&gt;\n\n&lt;script&gt;\nconst targetElement = document.getElementById(\"targetElement\");\n\ntargetElement.addEventListener(\"customEvent\", function () {\n  console.log(\"Custom event triggered\");\n});\n\nconst customEvent = new Event(\"customEvent\", {\n  bubbles: true,\n  cancelable: true\n});\n\ntargetElement.dispatchEvent(customEvent);\n&lt;\/script&gt;\n<\/code><\/pre>\n\n\n\n<p>The complete flow is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Create event\n\u2192 Select target\n\u2192 Attach listener\n\u2192 Dispatch event\n\u2192 Event handler runs\n<\/code><\/pre>\n\n\n\n<p>If you need to pass additional information with an event, use <code>CustomEvent()<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const userEvent = new CustomEvent(\"userLogin\", {\n  detail: {\n    username: \"Vaishali\"\n  }\n});\n\ndocument.dispatchEvent(userEvent);\n<\/code><\/pre>\n\n\n\n<p>The additional information can then be accessed through <code>event.detail<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>document.addEventListener(\"userLogin\", function (event) {\n  console.log(event.detail.username);\n});\n<\/code><\/pre>\n\n\n\n<p>Custom events are useful when different components of an application need to communicate without being tightly connected.<\/p>\n\n\n\n<p><em><strong>Also R<\/strong><\/em><strong>ead:<\/strong> <a href=\"https:\/\/www.guvi.in\/blog\/the-beginners-guide-to-javascript-closures\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>The Beginner\u2019s Guide To Closures In JavaScript<\/strong><\/a><\/p>\n\n\n\n<p>Now that you understand how events can be created and dispatched, the next step is learning the event types you will use most often in web applications.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Types of Events in JavaScript<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img decoding=\"async\" width=\"1200\" height=\"630\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/09\/image-342-1200x630.png\" alt=\"\" class=\"wp-image-138033\" style=\"aspect-ratio:1.9047619047619047;width:741px;height:auto\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/09\/image-342-1200x630.png 1200w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/09\/image-342-300x158.png 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/09\/image-342-768x403.png 768w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/09\/image-342-1536x806.png 1536w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/09\/image-342-2048x1075.png 2048w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/09\/image-342-150x79.png 150w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" title=\"\"><\/figure>\n\n\n\n<p>JavaScript supports many browser and user-generated events. The following are some of the most commonly used events in frontend development.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. Click Event<\/strong><\/h3>\n\n\n\n<p>The <code>click<\/code> event occurs when a user clicks an element.<\/p>\n\n\n\n<p>It is commonly used for:<\/p>\n\n\n\n<ul>\n<li>Buttons<\/li>\n\n\n\n<li>Menus<\/li>\n\n\n\n<li>Modals<\/li>\n\n\n\n<li>Navigation<\/li>\n\n\n\n<li>Interactive cards<\/li>\n<\/ul>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;button id=\"myButton\"&gt;Click Me&lt;\/button&gt;\n\n&lt;script&gt;\nconst button = document.getElementById(\"myButton\");\n\nbutton.addEventListener(\"click\", function () {\n  console.log(\"Button clicked\");\n});\n&lt;\/script&gt;\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Submit Event<\/strong><\/h3>\n\n\n\n<p>The <code>submit<\/code> event occurs when a form is submitted.<\/p>\n\n\n\n<p>It is commonly used for:<\/p>\n\n\n\n<ul>\n<li>Form validation<\/li>\n\n\n\n<li>Login forms<\/li>\n\n\n\n<li>Registration<\/li>\n\n\n\n<li>Search forms<\/li>\n\n\n\n<li>Sending data to APIs<\/li>\n<\/ul>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;form id=\"signupForm\"&gt;\n  &lt;input type=\"text\" id=\"name\" required&gt;\n  &lt;button type=\"submit\"&gt;Submit&lt;\/button&gt;\n&lt;\/form&gt;\n\n&lt;script&gt;\nconst form = document.getElementById(\"signupForm\");\n\nform.addEventListener(\"submit\", function (event) {\n  event.preventDefault();\n\n  const name = document.getElementById(\"name\").value;\n\n  console.log(\"Submitted by:\", name);\n});\n&lt;\/script&gt;\n<\/code><\/pre>\n\n\n\n<p><code>event.preventDefault()<\/code> prevents the browser from performing its normal form submission.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Keydown Event<\/strong><\/h3>\n\n\n\n<p>The <code>keydown<\/code> event fires when the user presses a keyboard key.<\/p>\n\n\n\n<p>It is useful for:<\/p>\n\n\n\n<ul>\n<li>Keyboard shortcuts<\/li>\n\n\n\n<li>Search boxes<\/li>\n\n\n\n<li>Games<\/li>\n\n\n\n<li>Form navigation<\/li>\n\n\n\n<li>Enter and Escape key handling<\/li>\n<\/ul>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;input id=\"searchInput\" type=\"text\" placeholder=\"Search\"&gt;\n\n&lt;script&gt;\nconst input = document.getElementById(\"searchInput\");\n\ninput.addEventListener(\"keydown\", function (event) {\n  console.log(\"Key pressed:\", event.key);\n\n  if (event.key === \"Enter\") {\n    console.log(\"Search started\");\n  }\n});\n&lt;\/script&gt;\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. Mouseover Event<\/strong><\/h3>\n\n\n\n<p>The <code>mouseover<\/code> event fires when the pointer enters an element or one of its child elements.<\/p>\n\n\n\n<p>It is commonly used for:<\/p>\n\n\n\n<ul>\n<li>Tooltips<\/li>\n\n\n\n<li>Dropdown menus<\/li>\n\n\n\n<li>Hover previews<\/li>\n\n\n\n<li>Image effects<\/li>\n\n\n\n<li>Interactive cards<\/li>\n<\/ul>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div id=\"card\"&gt;Hover over me&lt;\/div&gt;\n\n&lt;script&gt;\nconst card = document.getElementById(\"card\");\n\ncard.addEventListener(\"mouseover\", function () {\n  card.textContent = \"Mouse detected\";\n});\n&lt;\/script&gt;\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. Load Event<\/strong><\/h3>\n\n\n\n<p>The <code>load<\/code> event fires when a page or resource has finished loading.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>window.addEventListener(\"load\", function () {\n  console.log(\"Page loaded\");\n});\n<\/code><\/pre>\n\n\n\n<p>It can be useful when code depends on assets being completely available.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. Focus Event<\/strong><\/h3>\n\n\n\n<p>The <code>focus<\/code> event occurs when an element receives focus.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;input id=\"email\" type=\"email\"&gt;\n\n&lt;script&gt;\nconst email = document.getElementById(\"email\");\n\nemail.addEventListener(\"focus\", function () {\n  email.style.backgroundColor = \"lightblue\";\n});\n&lt;\/script&gt;\n<\/code><\/pre>\n\n\n\n<p>It is commonly used for:<\/p>\n\n\n\n<ul>\n<li>Input highlighting<\/li>\n\n\n\n<li>Form guidance<\/li>\n\n\n\n<li>Validation<\/li>\n\n\n\n<li>Accessibility improvements<\/li>\n<\/ul>\n\n\n\n<p><strong>Also Read:<\/strong> <a href=\"https:\/\/www.guvi.in\/blog\/best-reasons-to-learn-javascript\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>7 Best Reasons to Learn JavaScript | 1 Bonus Point<\/strong><\/a><\/p>\n\n\n\n<p>Understanding individual event types is only part of event handling. You also need to understand how an event moves through the DOM after it occurs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>JavaScript Event Phases<\/strong><\/h2>\n\n\n\n<p>A DOM event normally passes through three phases:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Capturing\n\u2192 Target\n\u2192 Bubbling\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. Event Capturing<\/strong><\/h3>\n\n\n\n<p><a href=\"https:\/\/www.guvi.in\/blog\/event-capturing-in-the-dom-tree\/\" target=\"_blank\" rel=\"noreferrer noopener\">Event capturing<\/a> is the phase where an event travels from the outermost ancestor toward the element that triggered it.<\/p>\n\n\n\n<p>Consider:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div id=\"parent\"&gt;\n  &lt;button id=\"child\"&gt;Click Me&lt;\/button&gt;\n&lt;\/div&gt;\n<\/code><\/pre>\n\n\n\n<p>During capturing:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>document\n   \u2193\nbody\n   \u2193\nparent\n   \u2193\nbutton\n<\/code><\/pre>\n\n\n\n<p>Capturing can be enabled with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>parent.addEventListener(\n  \"click\",\n  function () {\n    console.log(\"Parent capturing\");\n  },\n  { capture: true }\n);\n<\/code><\/pre>\n\n\n\n<p>You can learn more about the complete <a href=\"https:\/\/www.guvi.in\/blog\/dom-event-flow\/\" target=\"_blank\" rel=\"noreferrer noopener\">event flow<\/a> in the DOM.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Event Targeting<\/strong><\/h3>\n\n\n\n<p>The target phase occurs when the event reaches the element that originally triggered it.<\/p>\n\n\n\n<p>For example, if a user clicks:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;button id=\"child\"&gt;Click Me&lt;\/button&gt;\n<\/code><\/pre>\n\n\n\n<p>the button becomes the <code>event.target<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>child.addEventListener(\"click\", function (event) {\n  console.log(event.target);\n});\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Event Bubbling<\/strong><\/h3>\n\n\n\n<p><a href=\"https:\/\/www.guvi.in\/blog\/event-bubbling-in-dom\/\" target=\"_blank\" rel=\"noreferrer noopener\">Event bubbling<\/a> occurs after the target phase.<\/p>\n\n\n\n<p>The event travels upward through the element&#8217;s ancestors.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>button\n   \u2191\nparent\n   \u2191\nbody\n   \u2191\ndocument\n<\/code><\/pre>\n\n\n\n<p>Bubbling is the default behaviour for many common DOM events and is the foundation of event delegation.<\/p>\n\n\n\n<p><strong>Also Read:<\/strong> <a href=\"https:\/\/www.guvi.in\/blog\/best-javascript-practices-for-developers\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>7 Best JavaScript Practices Every Developer Must Follow<\/strong><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Event Bubbling vs Event Capturing- Visualized Explanation<\/strong><\/h2>\n\n\n\n<p>Event bubbling and capturing describe the direction in which an event travels through the DOM.<\/p>\n\n\n\n<p>Consider:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div id=\"parent\"&gt;\n  &lt;button id=\"child\"&gt;Click Me&lt;\/button&gt;\n&lt;\/div&gt;\n<\/code><\/pre>\n\n\n\n<p>The entire event flow looks like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CAPTURING\n\ndocument\n   \u2193\nbody\n   \u2193\nparent\n   \u2193\nbutton\n\nTARGET\n\n<\/code><\/pre>\n\n\n<p>[button]<\/p>\n\n\n\n<p>BUBBLING button \u2191 parent \u2191 body \u2191 document<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Event Capturing<\/strong><\/h3>\n\n\n\n<p>Capturing moves from the outer DOM toward the target.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Parent \u2192 Child\n<\/code><\/pre>\n\n\n\n<p>Use:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>parent.addEventListener(\n  \"click\",\n  function () {\n    console.log(\"Capturing\");\n  },\n  { capture: true }\n);\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Event Bubbling<\/strong><\/h3>\n\n\n\n<p>Bubbling starts at the target and moves toward its ancestors.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Child \u2192 Parent\n<\/code><\/pre>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>child.addEventListener(\"click\", function () {\n  console.log(\"Child clicked\");\n});\n\nparent.addEventListener(\"click\", function () {\n  console.log(\"Parent clicked\");\n});\n<\/code><\/pre>\n\n\n\n<p>Clicking the button produces:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Child clicked\nParent clicked\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th><strong>Factor<\/strong><\/th><th><strong>Event Bubbling<\/strong><\/th><th><strong>Event Capturing<\/strong><\/th><\/tr><\/thead><tbody><tr><td>Direction<\/td><td>Target \u2192 Ancestors<\/td><td>Ancestors \u2192 Target<\/td><\/tr><tr><td>Flow<\/td><td>Child \u2192 Parent<\/td><td>Parent \u2192 Child<\/td><\/tr><tr><td>Default behaviour<\/td><td>Common default<\/td><td>Must normally be enabled<\/td><\/tr><tr><td><code>addEventListener()<\/code><\/td><td>Default<\/td><td><code>{ capture: true }<\/code><\/td><\/tr><tr><td>Common use<\/td><td>Event delegation<\/td><td>Intercepting events earlier<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Also Read:<\/strong> <a href=\"https:\/\/www.guvi.in\/blog\/best-javascript-project-ideas\/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>30 Best JavaScript Project Ideas For You [3 Bonus Portfolio Projects]<\/strong><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Event Delegation Pattern- Why It Matters for Performance<\/strong><\/h2>\n\n\n\n<p>Event delegation means attaching one event listener to a parent rather than attaching separate listeners to every child element.<\/p>\n\n\n\n<p>Consider:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;ul id=\"productList\"&gt;\n  &lt;li&gt;&lt;button data-id=\"1\"&gt;Product 1&lt;\/button&gt;&lt;\/li&gt;\n  &lt;li&gt;&lt;button data-id=\"2\"&gt;Product 2&lt;\/button&gt;&lt;\/li&gt;\n  &lt;li&gt;&lt;button data-id=\"3\"&gt;Product 3&lt;\/button&gt;&lt;\/li&gt;\n&lt;\/ul&gt;\n<\/code><\/pre>\n\n\n\n<p>You could attach a separate listener to every button:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const buttons = document.querySelectorAll(\"#productList button\");\n\nbuttons.forEach(function (button) {\n  button.addEventListener(\"click\", function () {\n    console.log(\"Product clicked\");\n  });\n});\n<\/code><\/pre>\n\n\n\n<p>A more scalable approach is to use one listener on the parent:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const productList = document.getElementById(\"productList\");\n\nproductList.addEventListener(\"click\", function (event) {\n  if (event.target.matches(\"button\")) {\n    console.log(\"Product ID:\", event.target.dataset.id);\n  }\n});\n<\/code><\/pre>\n\n\n\n<p>This works because the button&#8217;s <code>click<\/code> event bubbles to <code>productList<\/code>.<\/p>\n\n\n\n<p>Instead of:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1,000 elements\n\u2192 1,000 listeners\n<\/code><\/pre>\n\n\n\n<p>you can often use:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1 parent\n\u2192 1 listener\n<\/code><\/pre>\n\n\n\n<p>Event delegation is particularly useful for:<\/p>\n\n\n\n<ul>\n<li>Lists<\/li>\n\n\n\n<li>Tables<\/li>\n\n\n\n<li>Menus<\/li>\n\n\n\n<li>Dynamic content<\/li>\n\n\n\n<li>Repeated components<\/li>\n\n\n\n<li>Large interfaces<\/li>\n<\/ul>\n\n\n\n<p>Another advantage is that dynamically added child elements can automatically work with the parent&#8217;s existing listener.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const newItem = document.createElement(\"li\");\n\nnewItem.innerHTML =\n  '&lt;button data-id=\"4\"&gt;Product 4&lt;\/button&gt;';\n\nproductList.appendChild(newItem);\n<\/code><\/pre>\n\n\n\n<p>No additional <code>click<\/code> listener is required for the new button.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Async Events- Promises vs Event Listeners<\/strong><\/h2>\n\n\n\n<p>Event listeners and Promises both deal with operations that may happen later, but they serve different purposes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Event Listeners<\/strong><\/h3>\n\n\n\n<p>An event listener waits for an event to occur.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>button.addEventListener(\"click\", function () {\n  console.log(\"Clicked\");\n});\n<\/code><\/pre>\n\n\n\n<p>The event may occur:<\/p>\n\n\n\n<ul>\n<li>Once<\/li>\n\n\n\n<li>Multiple times<\/li>\n\n\n\n<li>Much later<\/li>\n\n\n\n<li>Never<\/li>\n<\/ul>\n\n\n\n<p>The listener can continue responding whenever the event occurs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Promises<\/strong><\/h3>\n\n\n\n<p>A Promise represents the eventual completion or failure of an asynchronous operation.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fetch(\"\/api\/users\")\n  .then(function (response) {\n    return response.json();\n  })\n  .then(function (data) {\n    console.log(data);\n  })\n  .catch(function (error) {\n    console.error(error);\n  });\n<\/code><\/pre>\n\n\n\n<p>A Promise normally moves from:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Pending\n\u2192 Fulfilled\n<\/code><\/pre>\n\n\n\n<p>or:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Pending\n\u2192 Rejected\n<\/code><\/pre>\n\n\n\n<p>Once settled, the same Promise does not settle again.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th><strong>Factor<\/strong><\/th><th><strong>Event Listeners<\/strong><\/th><th><strong>Promises<\/strong><\/th><\/tr><\/thead><tbody><tr><td>Main purpose<\/td><td>Respond to events<\/td><td>Represent async completion<\/td><\/tr><tr><td>Frequency<\/td><td>Can fire repeatedly<\/td><td>Settles once<\/td><\/tr><tr><td>Example<\/td><td><code>click<\/code>, <code>keydown<\/code>, <code>submit<\/code><\/td><td>API request<\/td><\/tr><tr><td>Typical API<\/td><td><code>addEventListener()<\/code><\/td><td><code>Promise<\/code>, <code>fetch()<\/code>, <code>async\/await<\/code><\/td><\/tr><tr><td>Best for<\/td><td>Repeated interactions<\/td><td>One async result<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Using Events and Promises Together<\/strong><\/h3>\n\n\n\n<p>Real applications frequently combine both.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const button = document.getElementById(\"loadUsers\");\n\nbutton.addEventListener(\"click\", async function () {\n  try {\n    const response = await fetch(\"\/api\/users\");\n    const users = await response.json();\n\n    console.log(users);\n  } catch (error) {\n    console.error(\"Failed to load users:\", error);\n  }\n});\n<\/code><\/pre>\n\n\n\n<p>Here:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>User clicks button\n\u2192 click event fires\n\u2192 event listener executes\n\u2192 fetch() starts\n\u2192 Promise resolves\n\u2192 data is processed\n<\/code><\/pre>\n\n\n\n<p>In simple terms:<\/p>\n\n\n\n<p><strong>Event listeners \u2192 wait for events<\/strong><\/p>\n\n\n\n<p><strong>Promises \u2192 wait for asynchronous results<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Common JavaScript Event Interview Questions for Frontend Roles<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. What Is a JavaScript Event?<\/strong><\/h3>\n\n\n\n<p>A JavaScript event is an action or occurrence detected by the browser that can trigger JavaScript code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. What Does addEventListener() Do?<\/strong><\/h3>\n\n\n\n<p><code>addEventListener()<\/code> attaches an event listener to an element.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>button.addEventListener(\"click\", handleClick);\n<\/code><\/pre>\n\n\n\n<p>It allows multiple listeners to be attached without replacing existing listeners.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. What Is Event Bubbling?<\/strong><\/h3>\n\n\n\n<p>Event bubbling occurs when an event starts at its target and then travels upward through its ancestor elements.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. What Is Event Capturing?<\/strong><\/h3>\n\n\n\n<p>Event capturing occurs when an event travels from the outer ancestors toward the target element.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. What Is Event Delegation?<\/strong><\/h3>\n\n\n\n<p>Event delegation places an event listener on a parent and uses event bubbling to handle events from child elements.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. What Is the Difference Between event.target and event.currentTarget?<\/strong><\/h3>\n\n\n\n<p><code>event.target<\/code> is the element that originally triggered the event.<\/p>\n\n\n\n<p><code>event.currentTarget<\/code> is the element whose event listener is currently running.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>parent.addEventListener(\"click\", function (event) {\n  console.log(event.target);\n  console.log(event.currentTarget);\n});\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>7. What Does preventDefault() Do?<\/strong><\/h3>\n\n\n\n<p><code>preventDefault()<\/code> prevents the browser&#8217;s default action.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>form.addEventListener(\"submit\", function (event) {\n  event.preventDefault();\n});\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>8. What Does stopPropagation() Do?<\/strong><\/h3>\n\n\n\n<p><code>stopPropagation()<\/code> prevents an event from continuing through the propagation path.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>button.addEventListener(\"click\", function (event) {\n  event.stopPropagation();\n});\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>9. What Is the Difference Between onclick and addEventListener()?<\/strong><\/h3>\n\n\n\n<p><code>onclick<\/code> assigns a handler directly:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>button.onclick = handleClick;\n<\/code><\/pre>\n\n\n\n<p><code>addEventListener()<\/code> attaches a listener:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>button.addEventListener(\"click\", handleClick);\n<\/code><\/pre>\n\n\n\n<p><code>addEventListener()<\/code> is generally more flexible because it supports multiple listeners and options such as <code>capture<\/code>, <code>once<\/code> and <code>passive<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>10. Why Is Event Delegation Useful?<\/strong><\/h3>\n\n\n\n<p>Event delegation can reduce the number of listeners required for repeated elements and automatically works with dynamically added child elements.<\/p>\n\n\n\n<p>For more frontend interview preparation, read these <a href=\"https:\/\/www.guvi.in\/blog\/javascript-questions-towards-better-interviews\/\" target=\"_blank\" rel=\"noreferrer noopener\">JavaScript interview questions<\/a>.<\/p>\n\n\n\n<p><em>Ready to boost your<\/em> <a href=\"https:\/\/www.guvi.in\/blog\/career-opportunities-in-web-development\/\" target=\"_blank\" rel=\"noreferrer noopener\"><em>web development career<\/em><\/a><em>? Join HCL GUVI&#8217;s<\/em> <a href=\"https:\/\/www.guvi.in\/zen-class\/full-stack-development-course\/?utm_source=blog&amp;utm_medium=hyperlink&amp;utm_campaign=guide-for-events-in-javascript\" target=\"_blank\" rel=\"noreferrer noopener\"><em>Full Stack Development Course<\/em><\/a><em>! Learn both front-end and back-end skills through easy-to-follow projects and expert tips. This course covers everything from basics to advanced web apps. Enroll today!<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>JavaScript events form the foundation of interactive web applications. They allow browsers to respond to actions such as clicks, keyboard input, form submissions and mouse interactions without requiring the page to reload.<\/p>\n\n\n\n<p>Start by understanding common events and <code>addEventListener()<\/code>. Then learn how capturing, targeting and bubbling control event propagation. Once those concepts are clear, patterns such as event delegation and asynchronous event handling become much easier to understand.<\/p>\n\n\n\n<p>The best way to master JavaScript events is to build small interactive components and experiment with how events travel through the DOM.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>FAQs<\/strong><\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1788299730277\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What Are the Most Common JavaScript Events?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>The most common JavaScript events include <code>click<\/code>, <code>submit<\/code>, <code>keydown<\/code>, <code>keyup<\/code>, <code>mouseover<\/code>, <code>focus<\/code>, <code>change<\/code>, <code>input<\/code> and <code>load<\/code>. These events allow developers to respond to user actions and browser activity.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1788299750520\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What Is addEventListener() in JavaScript?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p><code>addEventListener()<\/code> is a JavaScript method used to attach an event listener to an element. It allows code to run whenever a specified event occurs.<br \/><code>button.addEventListener(\"click\", function () { console.log(\"Button clicked\"); });<\/code><\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1788299765220\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What Is the Difference Between Event Bubbling and Event Capturing?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Event bubbling moves from the target element upward through its parent elements. Event capturing moves from outer parent elements down toward the target. Bubbling is the default phase used by most event listeners.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1788299778721\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What Is Event Delegation in JavaScript?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Event delegation is a pattern where one event listener is added to a parent element instead of adding separate listeners to every child element. It uses event bubbling and is especially useful for dynamic lists, tables and repeated UI components.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1788299786837\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \">What Is the Difference Between JavaScript Events and Promises?<\/h3>\n<div class=\"rank-math-answer \">\n\n<p>JavaScript events respond to actions that can occur repeatedly, such as clicks or key presses. Promises represent the completion or failure of an asynchronous operation and settle only once. Event listeners are commonly used for user interactions, while Promises are used for operations such as API requests.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Every interactive action on a website starts with an event. A click can open a menu. A keypress can trigger a search. A form submission can send user data. Even moving the mouse over an element can change what appears on the screen. In JavaScript, events make these interactions possible by allowing the browser to [&hellip;]<\/p>\n","protected":false},"author":60,"featured_media":138034,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[294],"tags":[],"views":"18272","authorinfo":{"name":"Vaishali","url":"https:\/\/www.guvi.in\/blog\/author\/vaishali\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2026\/09\/IMG_4292-300x116.png","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/43056"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/60"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=43056"}],"version-history":[{"count":45,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/43056\/revisions"}],"predecessor-version":[{"id":138035,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/43056\/revisions\/138035"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/138034"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=43056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=43056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=43056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}