{"id":128,"date":"2026-05-05T09:30:10","date_gmt":"2026-05-05T09:30:10","guid":{"rendered":"https:\/\/gigz.pk\/javascript\/?post_type=lesson&#038;p=128"},"modified":"2026-05-05T09:30:11","modified_gmt":"2026-05-05T09:30:11","slug":"events-and-event-listeners","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/javascript\/?lesson=events-and-event-listeners","title":{"rendered":"Events and Event Listeners"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Introduction<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Events and Event Listeners are core concepts in JavaScript that allow websites to respond to user actions. They make web pages interactive by detecting actions such as clicks typing scrolling and mouse movements. Understanding events is essential for building dynamic and user friendly applications<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Understanding Events<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An event is any action that happens in the browser. This action can be performed by the user or triggered by the system<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Common examples of events include clicking a button pressing a key submitting a form loading a page or moving the mouse<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Event Listeners<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An event listener is a function that waits for a specific event to occur and then executes code in response. It connects an event to a function so that when the event happens the function runs automatically<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Basic Syntax<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is a simple example of using an event listener<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>document.getElementById(\"btn\").addEventListener(\"click\", function() {<br>  alert(\"Button clicked\");<br>});<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this example the event listener waits for a click on the button and then shows a message<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Common Types of Events<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Click Event<br>Triggered when a user clicks on an element<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Mouse Events<br>Include mouseover mouseout and mousemove<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Keyboard Events<br>Triggered when keys are pressed or released<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Form Events<br>Include submit change and focus events<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Window Events<br>Triggered when the page loads resizes or scrolls<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Why Event Listeners Are Important<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">They make websites interactive and responsive<br>They improve user experience by reacting instantly<br>They allow developers to control user actions<br>They help in building dynamic applications<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Best Practices<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use addEventListener instead of inline events for better structure<br>Keep functions clean and reusable<br>Avoid adding too many listeners unnecessarily<br>Remove event listeners when they are no longer needed<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Conclusion<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Events and event listeners are essential for creating interactive websites. By understanding how they work you can build responsive applications that react to user actions efficiently<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/javascript\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Intermediate JavaScript > DOM Manipulation > Events and Event Listeners<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1777973384689\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1777973384419\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"menu_order":0,"template":"","class_list":["post-128","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Events and Event Listeners - Learn JavaScript with GIGZ.PK<\/title>\n<meta name=\"description\" content=\"Learn JavaScript events and event listeners to build interactive websites and handle user actions with simple examples easily\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/javascript\/?lesson=events-and-event-listeners\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Events and Event Listeners - Learn JavaScript with GIGZ.PK\" \/>\n<meta property=\"og:description\" content=\"Learn JavaScript events and event listeners to build interactive websites and handle user actions with simple examples easily\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/javascript\/?lesson=events-and-event-listeners\" \/>\n<meta property=\"og:site_name\" content=\"Learn JavaScript with GIGZ.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-05T09:30:11+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=events-and-event-listeners\",\"url\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=events-and-event-listeners\",\"name\":\"Events and Event Listeners - Learn JavaScript with GIGZ.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/#website\"},\"datePublished\":\"2026-05-05T09:30:10+00:00\",\"dateModified\":\"2026-05-05T09:30:11+00:00\",\"description\":\"Learn JavaScript events and event listeners to build interactive websites and handle user actions with simple examples easily\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=events-and-event-listeners#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=events-and-event-listeners\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?lesson=events-and-event-listeners#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/javascript\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Intermediate JavaScript > DOM Manipulation > Events and Event Listeners\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/\",\"name\":\"Learn JavaScript with GIGZ.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/javascript\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Events and Event Listeners - Learn JavaScript with GIGZ.PK","description":"Learn JavaScript events and event listeners to build interactive websites and handle user actions with simple examples easily","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/javascript\/?lesson=events-and-event-listeners","og_locale":"en_US","og_type":"article","og_title":"Events and Event Listeners - Learn JavaScript with GIGZ.PK","og_description":"Learn JavaScript events and event listeners to build interactive websites and handle user actions with simple examples easily","og_url":"https:\/\/gigz.pk\/javascript\/?lesson=events-and-event-listeners","og_site_name":"Learn JavaScript with GIGZ.PK","article_modified_time":"2026-05-05T09:30:11+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/javascript\/?lesson=events-and-event-listeners","url":"https:\/\/gigz.pk\/javascript\/?lesson=events-and-event-listeners","name":"Events and Event Listeners - Learn JavaScript with GIGZ.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/javascript\/#website"},"datePublished":"2026-05-05T09:30:10+00:00","dateModified":"2026-05-05T09:30:11+00:00","description":"Learn JavaScript events and event listeners to build interactive websites and handle user actions with simple examples easily","breadcrumb":{"@id":"https:\/\/gigz.pk\/javascript\/?lesson=events-and-event-listeners#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/javascript\/?lesson=events-and-event-listeners"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/javascript\/?lesson=events-and-event-listeners#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/javascript"},{"@type":"ListItem","position":2,"name":"Intermediate JavaScript > DOM Manipulation > Events and Event Listeners"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/javascript\/#website","url":"https:\/\/gigz.pk\/javascript\/","name":"Learn JavaScript with GIGZ.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/javascript\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=\/wp\/v2\/lesson\/128","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/javascript\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}