Hosting Solutions
mobile_logo Table of Contents

Best HTML Event Handling addEventListener Tutorial

Learn how to use the JavaScript addEventListener() method for an optimal way of adding user interactivity into your websites and web applications that apply JavaScript. It cleans up your HTML markup and may be the only way to attach events to elements in the future. Modern Version <script> function addListeners(){ document.getElementById('mybtn').addEventListener("click", btn1func); document.getElementById('mybtn2').addEventListener("mouseover", btn2func); function btn1func(){ alert(" : mouse-click makes script run"); } function btn2func(){ alert(" : mouse-over makes script run"); } } window.addEventListener("load", addListeners); </script> <button id="mybtn">Click Button</button> <button id="mybtn2">Hover Event</button> Older Version (supports versions of Internet Explorer previous to IE9) <!DOCTYPE html> <html> <head> <script> function addListeners(){ if(window.addEventListener) { document.getElementById('mybtn').addEventListener("click",btn1func,false); document.getElementById('mybtn2').addEventListener("mouseover",btn2func,false); } else if (window.attachEvent){ // Added For Inetenet Explorer versions previous to IE9 document.getElementById('mybtn').attachEvent("onclick", btn1func); document.getElementById('mybtn2').attachEvent("onmouseover",btn2func); } // Write your functions here function btn1func(){ alert(" : mouse-click makes script run"); } function btn2func(){ alert(" : mouse-over makes script run"); } } window.onload = addListeners; </script> </head> <body> <button id="mybtn">Click Button</button> <button id="mybtn2">Hover Event</button> </body> </html>

JavaScript Videos

General Programming

Flat Icons HTML Code Symbols Discovery JavaScriptHow to Program Code Syntax Highlighter Using JavaScriptFat Arrow Functions JavaScript Programming TutorialJavaScript Arguments Object Function TutorialJavaScript Storage Interface sessionStorage localStorage TutorialMulti Column Layout Grid Programming TutorialCustom Alert Box Programming TutorialCustom Confirm Box Programming TutorialCustom Prompt Box Programming TutorialChange CSS Class Style className Toggle TutorialPartial Print Document Tutorial HTML div ContentClick Outside Close Menu Box TutorialPercent Math Calculations Programming TutorialDual Image Reveal Control TutorialPage Loading Screen Document Preloader TutorialControl Panel Flyout Menu Windows AnimationsCustom Data Attributes HTML JavaScript CSS TutorialFullscreen API JavaScript Code Examples and SpecificationConsole Log Tutorial Chrome Firefox IE BrowsersToggle Function Click Drop Down Menus TutorialChange Tab Text and Window Title On The Fly TutorialDice Roll Programming Tutorial For Web Browser GamesDynamic Lorem Ipsum Placeholder Text TutorialBest HTML Event Handling addEventListener TutorialsetAttribute removeAttribute Methods TutorialDisabled User Handling Redirect WebsiteCapture Keyboard Input Tutorial HTML ApplicationCapture Keyboard Input Condition Logic TutorialChange Style Sheet Using Tutorial CSS Swap StylesheetWeb Page Overlay Tutorial Transparent CoverLights Out For Earth Day Tutorial Dark Page SwitchWindow Size Responsive CSS Layout Stylesheet Change TutorialRead Mouse Coordinates Pointer Position Tutorial