{"id":196,"date":"2020-10-22T21:39:00","date_gmt":"2020-10-22T21:39:00","guid":{"rendered":""},"modified":"2024-09-13T15:04:51","modified_gmt":"2024-09-13T15:04:51","slug":"javascript-tutorial-series-dom-events","status":"publish","type":"post","link":"https:\/\/zofxare.com\/zofxare\/blog\/2020\/10\/22\/javascript-tutorial-series-dom-events\/","title":{"rendered":"JavaScript Tutorial Series &#8211; DOM Events"},"content":{"rendered":"<p>In JavaScript, DOM events capture actions a user performs on an HTML element in a web application. Examples of user actions include clicking on a button or link, entering text into an input field or selecting an item from a list. To capture a user event, JavaScript has action listeners. Action listeners define a callback function that performs a certain set of instructions when an event occurs on a given HTML element. HTML elements have HTML event attributes. The following is a list of commonly used event attributes to capture mouse and keyboard events:<\/p>\n<p>onclick<br \/>\nondblclick<br \/>\nonmousedown<br \/>\nonmousemove<br \/>\nonmouseout<br \/>\nonmouseover<br \/>\nonmouseup<br \/>\nonkeydown<br \/>\nonkeypress<br \/>\nonkeyup<\/p>\n<p>The following is an example to perform a specific action when a button is clicked:<\/p>\n<pre><code>\n\nIn an HTML file, define the button:\n\n  &lt;button id='testbutton'&gt;Test&lt;\/button&gt;\n\n\nIn a JavaScript file, define the event handler:\n\n  document.getElementById('testbutton').onclick = function(evt) {\n    console.log(evt.target.id + ' was clicked');\n  }\n\n<\/code><\/pre>\n<p>The evt variable is the HTML element that received the action. It can be used to access information about the HTML element.<\/p>\n<p>In the followng example, the button is changed to a div and the same click handler added. The div can then be styled to create a custom button:<\/p>\n<pre><code>\n\nIn an HTML file, define the div:\n\n  &lt;div id='testbuttondiv'&gt;Test&lt;\/div&gt;\n\n\nIn a JavaScript file, define the event handler:\n\n  document.getElementById('testbuttondiv').onclick = function(evt) {\n    console.log(evt.target.id + ' was clicked');\n  }\n<\/code><\/pre>\n<p>Event handlers may also be defined using the addEventListener function.<br \/>\nExample:<\/p>\n<pre><code>\n\nIn an HTML file, define the div:\n\n  &lt;div id='testbuttondiv'&gt;Test&lt;\/div&gt;\n\n\nIn a JavaScript file, define the event handler:\n\n  document.getElementById('testbuttondiv').addEventListener('click', function(evt) {\n    console.log(evt.target.id + ' was clicked');\n  }, false);\n  \n<\/code><\/pre>\n<p>The event handler function may also be defined directly on the HTML element as shown below:<\/p>\n<pre><code>\n\nIn an HTML file, define the div:\n\n  &lt;div id='testbuttondiv' onclick=\"console.log(testbuttondiv was clicked');\" &gt;Test&lt;\/div&gt;\n  \n<\/code><\/pre>\n<p>I usually use the first approach since it is simpler and easier to read. I don&#8217;t use the third method because it is cleaner to put JavaScript code in a separate .js file rather than inlined inside the HTML.<\/p>\n<p>A complete list of HTML event attributes can be found here: <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/Events\" target=\"_blank\" rel=\"noopener\">https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/Events<\/a><\/p>\n<p>(#sponsored)<\/br><br \/>\n<a href=\"https:\/\/amzn.to\/39aSKtV\" rel=\"noopener noreferrer\" target=\"_blank\">More JavaScript<\/a><\/br><\/br><\/p>\n<p><iframe frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" scrolling=\"no\" src=\"\/\/ws-na.amazon-adsystem.com\/widgets\/q?ServiceVersion=20070822&amp;OneJS=1&amp;Operation=GetAdHtml&amp;MarketPlace=US&amp;source=ss&amp;ref=as_ss_li_til&amp;ad_type=product_link&amp;tracking_id=zofxare-20&amp;language=en_US&amp;marketplace=amazon&amp;region=US&amp;placement=144934013X&amp;asins=144934013X&amp;linkId=1c564951ff9e32885fd78e6d3e51cd88&amp;show_border=true&amp;link_opens_in_new_window=true\" style=\"height: 240px; width: 120px;\"><\/iframe><iframe frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" scrolling=\"no\" src=\"\/\/ws-na.amazon-adsystem.com\/widgets\/q?ServiceVersion=20070822&amp;OneJS=1&amp;Operation=GetAdHtml&amp;MarketPlace=US&amp;source=ss&amp;ref=as_ss_li_til&amp;ad_type=product_link&amp;tracking_id=zofxare-20&amp;language=en_US&amp;marketplace=amazon&amp;region=US&amp;placement=1119366445&amp;asins=1119366445&amp;linkId=dd23c2d87c50139ea87f57684a12c407&amp;show_border=true&amp;link_opens_in_new_window=true\" style=\"height: 240px; width: 120px;\"><\/iframe><iframe frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" scrolling=\"no\" src=\"\/\/ws-na.amazon-adsystem.com\/widgets\/q?ServiceVersion=20070822&amp;OneJS=1&amp;Operation=GetAdHtml&amp;MarketPlace=US&amp;source=ss&amp;ref=as_ss_li_til&amp;ad_type=product_link&amp;tracking_id=zofxare-20&amp;language=en_US&amp;marketplace=amazon&amp;region=US&amp;placement=0596517742&amp;asins=0596517742&amp;linkId=6d3fc10c6f7129dfd9a699a17a1caa01&amp;show_border=true&amp;link_opens_in_new_window=true\" style=\"height: 240px; width: 120px;\"><\/iframe><iframe frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" scrolling=\"no\" src=\"\/\/ws-na.amazon-adsystem.com\/widgets\/q?ServiceVersion=20070822&amp;OneJS=1&amp;Operation=GetAdHtml&amp;MarketPlace=US&amp;source=ss&amp;ref=as_ss_li_til&amp;ad_type=product_link&amp;tracking_id=zofxare-20&amp;language=en_US&amp;marketplace=amazon&amp;region=US&amp;placement=1497408180&amp;asins=1497408180&amp;linkId=13300f4284c721f7e5b0da78795da9eb&amp;show_border=true&amp;link_opens_in_new_window=true\" style=\"height: 240px; width: 120px;\"><\/iframe><\/p>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In JavaScript, DOM events capture actions a user performs on an HTML element in a web application. Examples of user actions include clicking on a button or link, entering text into an input field or selecting an item from a list. To capture a user event, JavaScript has action listeners. Action listeners define a callback [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","om_disable_all_campaigns":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[340,339,341],"tags":[],"class_list":["post-196","post","type-post","status-publish","format-standard","hentry","category-javascript","category-javascript-tutorials","category-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>JavaScript Tutorial Series - DOM Events - Zofxare Blog Home<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/zofxare.com\/zofxare\/blog\/2020\/10\/22\/javascript-tutorial-series-dom-events\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Tutorial Series - DOM Events - Zofxare Blog Home\" \/>\n<meta property=\"og:description\" content=\"In JavaScript, DOM events capture actions a user performs on an HTML element in a web application. Examples of user actions include clicking on a button or link, entering text into an input field or selecting an item from a list. To capture a user event, JavaScript has action listeners. Action listeners define a callback [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/zofxare.com\/zofxare\/blog\/2020\/10\/22\/javascript-tutorial-series-dom-events\/\" \/>\n<meta property=\"og:site_name\" content=\"Zofxare Blog Home\" \/>\n<meta property=\"article:published_time\" content=\"2020-10-22T21:39:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-09-13T15:04:51+00:00\" \/>\n<meta name=\"author\" content=\"adminuser\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"adminuser\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2020\\\/10\\\/22\\\/javascript-tutorial-series-dom-events\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2020\\\/10\\\/22\\\/javascript-tutorial-series-dom-events\\\/\"},\"author\":{\"name\":\"adminuser\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#\\\/schema\\\/person\\\/488c98b837076eb349075c14ea0e87d8\"},\"headline\":\"JavaScript Tutorial Series &#8211; DOM Events\",\"datePublished\":\"2020-10-22T21:39:00+00:00\",\"dateModified\":\"2024-09-13T15:04:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2020\\\/10\\\/22\\\/javascript-tutorial-series-dom-events\\\/\"},\"wordCount\":264,\"commentCount\":0,\"articleSection\":[\"javascript\",\"javascript tutorials\",\"tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2020\\\/10\\\/22\\\/javascript-tutorial-series-dom-events\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2020\\\/10\\\/22\\\/javascript-tutorial-series-dom-events\\\/\",\"url\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2020\\\/10\\\/22\\\/javascript-tutorial-series-dom-events\\\/\",\"name\":\"JavaScript Tutorial Series - DOM Events - Zofxare Blog Home\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#website\"},\"datePublished\":\"2020-10-22T21:39:00+00:00\",\"dateModified\":\"2024-09-13T15:04:51+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#\\\/schema\\\/person\\\/488c98b837076eb349075c14ea0e87d8\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2020\\\/10\\\/22\\\/javascript-tutorial-series-dom-events\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2020\\\/10\\\/22\\\/javascript-tutorial-series-dom-events\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2020\\\/10\\\/22\\\/javascript-tutorial-series-dom-events\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript Tutorial Series &#8211; DOM Events\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/\",\"name\":\"Zofxare Blog Home\",\"description\":\"The Zofxare blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#\\\/schema\\\/person\\\/488c98b837076eb349075c14ea0e87d8\",\"name\":\"adminuser\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g\",\"caption\":\"adminuser\"},\"sameAs\":[\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\"],\"url\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/author\\\/adminuser\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JavaScript Tutorial Series - DOM Events - Zofxare Blog Home","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:\/\/zofxare.com\/zofxare\/blog\/2020\/10\/22\/javascript-tutorial-series-dom-events\/","og_locale":"en_US","og_type":"article","og_title":"JavaScript Tutorial Series - DOM Events - Zofxare Blog Home","og_description":"In JavaScript, DOM events capture actions a user performs on an HTML element in a web application. Examples of user actions include clicking on a button or link, entering text into an input field or selecting an item from a list. To capture a user event, JavaScript has action listeners. Action listeners define a callback [&hellip;]","og_url":"https:\/\/zofxare.com\/zofxare\/blog\/2020\/10\/22\/javascript-tutorial-series-dom-events\/","og_site_name":"Zofxare Blog Home","article_published_time":"2020-10-22T21:39:00+00:00","article_modified_time":"2024-09-13T15:04:51+00:00","author":"adminuser","twitter_card":"summary_large_image","twitter_misc":{"Written by":"adminuser","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/zofxare.com\/zofxare\/blog\/2020\/10\/22\/javascript-tutorial-series-dom-events\/#article","isPartOf":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2020\/10\/22\/javascript-tutorial-series-dom-events\/"},"author":{"name":"adminuser","@id":"https:\/\/zofxare.com\/zofxare\/blog\/#\/schema\/person\/488c98b837076eb349075c14ea0e87d8"},"headline":"JavaScript Tutorial Series &#8211; DOM Events","datePublished":"2020-10-22T21:39:00+00:00","dateModified":"2024-09-13T15:04:51+00:00","mainEntityOfPage":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2020\/10\/22\/javascript-tutorial-series-dom-events\/"},"wordCount":264,"commentCount":0,"articleSection":["javascript","javascript tutorials","tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/zofxare.com\/zofxare\/blog\/2020\/10\/22\/javascript-tutorial-series-dom-events\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/zofxare.com\/zofxare\/blog\/2020\/10\/22\/javascript-tutorial-series-dom-events\/","url":"https:\/\/zofxare.com\/zofxare\/blog\/2020\/10\/22\/javascript-tutorial-series-dom-events\/","name":"JavaScript Tutorial Series - DOM Events - Zofxare Blog Home","isPartOf":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/#website"},"datePublished":"2020-10-22T21:39:00+00:00","dateModified":"2024-09-13T15:04:51+00:00","author":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/#\/schema\/person\/488c98b837076eb349075c14ea0e87d8"},"breadcrumb":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2020\/10\/22\/javascript-tutorial-series-dom-events\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/zofxare.com\/zofxare\/blog\/2020\/10\/22\/javascript-tutorial-series-dom-events\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/zofxare.com\/zofxare\/blog\/2020\/10\/22\/javascript-tutorial-series-dom-events\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/zofxare.com\/zofxare\/blog\/"},{"@type":"ListItem","position":2,"name":"JavaScript Tutorial Series &#8211; DOM Events"}]},{"@type":"WebSite","@id":"https:\/\/zofxare.com\/zofxare\/blog\/#website","url":"https:\/\/zofxare.com\/zofxare\/blog\/","name":"Zofxare Blog Home","description":"The Zofxare blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/zofxare.com\/zofxare\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/zofxare.com\/zofxare\/blog\/#\/schema\/person\/488c98b837076eb349075c14ea0e87d8","name":"adminuser","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g","caption":"adminuser"},"sameAs":["https:\/\/zofxare.com\/zofxare\/blog"],"url":"https:\/\/zofxare.com\/zofxare\/blog\/author\/adminuser\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts\/196","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/comments?post=196"}],"version-history":[{"count":1,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts\/196\/revisions"}],"predecessor-version":[{"id":229,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts\/196\/revisions\/229"}],"wp:attachment":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/media?parent=196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/categories?post=196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/tags?post=196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}