https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/Scripting/Events * Skip to main content * Skip to search * Skip to select language Open main menu * ReferencesReferences + Overview / Web Technology Web technology reference for developers + HTML Structure of content on the web + CSS Code used to describe document style + JavaScript General-purpose scripting language + HTTP Protocol for transmitting web resources + Web APIs Interfaces for building web applications + Web Extensions Developing extensions for web browsers + Accessibility Build web projects usable for all + Web Technology Web technology reference for developers * LearnLearn + Overview / MDN Learning Area Learn web development + MDN Learning Area Learn web development + HTML Learn to structure web content with HTML + CSS Learn to style content using CSS + JavaScript Learn to run scripts in the browser + Accessibility Learn to make the web accessible to all * PlusPlus + Overview A customized MDN experience + AI Help Get real-time assistance and support + Updates All browser compatibility updates at a glance + Documentation Learn how to use MDN Plus + FAQ Frequently asked questions about MDN Plus * Curriculum ^New * Blog * Tools + Playground Write, test and share your code + HTTP Observatory Scan a website for free + AI Help Get real-time assistance and support Search MDN[ ]Clear search inputSearch Theme * Log in * Sign up for free 1. Learn 2. Core learning modules 3. JavaScript 4. Events Article Actions * English (US) + [ ]Remember language + Deutsch + Espanol + Francais + Ri Ben Yu + hangugeo + Portugues (do Brasil) + Russkii + Zhong Wen (Jian Ti ) Filter sidebar[ ]Clear filter input In this article * What is an event? * Using addEventListener() * Other event listener mechanisms * Event objects * Preventing default behavior * It's not just web pages * Summary 1. Getting started modules 2. Environment setup 1. Installing software 2. Browsing the web 3. Code editors 4. Dealing with files 5. Command line 3. Your first website 1. What will it look like? 2. Creating the content 3. Styling the content 4. Adding interactivity 5. Publishing 4. Web standards 1. How the web works 2. The web standards model 3. How browsers load websites 5. Soft skills 1. Research and learning 2. Collaboration and teamwork 3. Workflows and processes 4. Finding a job 6. Core modules 7. Structuring content with HTML 1. Basic HTML syntax 2. Web page metadata 3. Headings and paragraphs 4. Emphasis and importance 5. Lists 6. Structuring documents 7. Advanced text features 8. Creating links 9. Challenge: Letter markup 10. Challenge: Bird watching site 11. Images 12. Video and audio 13. Challenge: Mozilla splash page 14. Table basics 15. Table accessibility 16. Challenge: Planet data table 17. Forms and buttons 18. Debugging HTML 19. Test your skills 1. HTML text basics 2. Advanced HTML text 3. Links 4. Images 5. Audio and video 6. Forms and buttons 8. CSS styling basics 1. What is CSS? 2. CSS getting started 3. Challenge: Biography page 4. Basic selectors 5. Attribute selectors 6. Pseudo-classes and elements 7. Combinators 8. Box model 9. Handling conflicts 10. Values and units 11. Sizing 12. Backgrounds and borders 13. Overflow 14. Images, media, forms 15. Styling tables 16. Debugging CSS 17. Challenge: Business card 18. Challenge: Fancy letterhead 19. Challenge: Fancy box styles 20. Test your skills 1. Backgrounds and borders 2. Box model 3. Cascade 4. Images and forms 5. Overflow 6. Selectors 7. Sizing 8. Values and units 9. CSS text styling 1. Text and font fundamentals 2. Styling lists 3. Styling links 4. Web fonts 5. Challenge: Community school homepage 10. CSS layout 1. Introduction 2. Floats 3. Positioning 4. Flexbox 5. CSS grid layout 6. Responsive web design 7. Media queries 8. Challenge: Fundamental layout 9. Test your skills 1. Flexbox 2. Floats 3. Grid 4. Positioning 5. Responsive design and media queries 11. Dynamic scripting with JavaScript 1. What is JavaScript? 2. JavaScript walkthrough 3. Troubleshooting 4. Variables 5. Numbers and operators 6. Strings 7. String methods 8. Arrays 9. Challenge: Story generator 10. Conditionals 11. Loops 12. Functions 13. Build your own function 14. Function return values 15. Events 16. Event bubbling 17. Challenge: Image gallery 18. Objects 19. DOM scripting 20. Network requests 21. JSON 22. Debugging and error handling 23. Test your skills 1. Arrays 2. Conditionals 3. Events 4. Functions 5. JSON 6. Loops 7. Math 8. Objects 9. Strings 10. Variables 12. JavaScript frameworks and libraries 1. Introduction 2. Framework features 3. React getting started 4. React ToDo app 5. React components 6. React events and state 7. React editing, filtering, conditional UI 8. React accessibility 9. React resources 13. Accessibility 1. What is accessibility? 2. Accessibility tools 3. Accessible HTML 4. Accessible CSS and JS 5. WAI-ARIA 6. Accessible multimedia 7. Mobile accessibility 8. Challenge: A11y debugging 9. Test your skills 1. CSS and JavaScript 2. HTML 3. WAI-ARIA 14. Design for developers 15. Version control 16. Extension modules 17. Advanced JavaScript objects 1. Object prototypes 2. Object-oriented programming 3. Classes in JavaScript 4. Object building practice 5. Challenge: Bouncing balls features 6. Test your skills 1. Object-oriented JavaScript 18. Client-side web APIs 1. Introduction 2. Video and audio 3. Drawing graphics 4. Client-side storage 5. Third-party APIs 19. Asynchronous JavaScript 1. Introduction 2. Using promises 3. Implementing promise-based APIs 4. Introducing workers 5. Challenge: Animation sequence 20. Web forms 1. Your first form 2. How to structure a web form 3. Basic native form controls 4. The HTML5 input types 5. Other form controls 6. Styling web forms 7. Advanced form styling 8. Customizable selects 9. UI pseudo-classes 10. Client-side form validation 11. Sending form data 21. Understanding client-side tools 1. Overview 2. Package management 3. Sample toolchain 4. Deploying our app 22. Server-side websites 1. First steps 1. Introduction 2. Client-server overview 3. Server-side frameworks 4. Website security 2. Django (Python) 1. Django introduction 2. Dev environment setup 3. 1: Local library tutorial 4. 2: Skeleton website 5. 3: Models 6. 4: Django admin site 7. 5: Home page 8. 6: Generic list and detail views 9. 7: Sessions framework 10. 8: Authentication and permissions 11. 9: Forms 12. 10: Testing 13. 11: Deploying 14. Django security 15. Challenge: Django blog 3. Express (Node.js) 1. Express/Node introduction 2. Dev environment setup 3. 1: Local library tutorial 4. 2: Skeleton website 5. 3: Using databases with Mongoose 6. 4: Routes and controllers 7. 5: Displaying data 8. 6: Working with forms 9. 7: Deploying 23. Web performance 1. The "why" of web performance 2. What is web performance? 3. Perceived performance 4. Measuring performance 5. Multimedia: Images 6. Multimedia: video 7. Performant JavaScript 8. Performant HTML 9. Performant CSS 10. Performance business case 24. Testing 1. Introduction 2. Testing strategies 3. Common HTML and CSS problems 4. Feature detection 5. Automated testing 6. Automation environment setup 25. Further resources 26. How to solve common problems 1. Common CSS problems 2. Common HTML problems 3. Common JavaScript problems 4. Design and accessibility 5. Tools and setup 6. Web mechanics 27. About 28. Resources for educators 29. Changelog In this article * What is an event? * Using addEventListener() * Other event listener mechanisms * Event objects * Preventing default behavior * It's not just web pages * Summary Introduction to events * Previous * Overview: Dynamic scripting with JavaScript * Next Events are things that happen in the system you are programming, which the system tells you about so your code can react to them. For example, if the user clicks a button on a webpage, you might want to react to that action by displaying an information box. In this article, we discuss some important concepts surrounding events, and look at the fundamentals of how they work in browsers. An understanding of HTML and the fundamentals of CSS, Prerequisites: familiarity with JavaScript basics as covered in previous lessons. * What events are -- a signal fired by the browser when something significant happens, which the developer can run some code in response to. Learning * Setting up event handlers using addEventListener() outcomes: (and removeEventListener()) and event handler properties. * Inline event handler attributes, and why you shouldn't use them. * Event objects. What is an event? Events are things that happen in the system you are programming -- the system produces (or "fires") a signal of some kind when an event occurs, and provides a mechanism by which an action can be automatically taken (that is, some code running) when the event occurs. Events are fired inside the browser window, and tend to be attached to a specific item that resides in it. This might be a single element, a set of elements, the HTML document loaded in the current tab, or the entire browser window. There are many different types of events that can occur. For example: * The user selects, clicks, or hovers the cursor over a certain element. * The user presses a key on the keyboard. * The user resizes or closes the browser window. * A web page finishes loading. * A form is submitted. * A video is played, paused, or ends. * An error occurs. You can gather from this (and from glancing at the event index) that there are a lot of events that can be fired. To react to an event, you attach an event listener to it. This is a code feature that listens out for the event firing. When the event fires, an event handler function (referenced by, or contained inside the event listener) is called to respond to the event firing. When such a block of code is set up to run in response to an event, we say we are registering an event handler. An example: handling a click event In the following example, we have a single button { margin: 10px; } Then we have some JavaScript. We'll look at this in more detail in the next section, but for now we can just say: it adds an event listener to the button's "click" event, and the contained handler function reacts to the event by setting the page background to a random color: js const btn = document.querySelector("button"); function random(number) { return Math.floor(Math.random() * (number + 1)); } btn.addEventListener("click", () => { const rndCol = `rgb(${random(255)} ${random(255)} ${random(255)})`; document.body.style.backgroundColor = rndCol; }); The example output is as follows. Try clicking the button: Using addEventListener() As we saw in the last example, objects that can fire events have an addEventListener() method, and this is the recommended mechanism for adding event listeners. Let's take a closer look at the code from the last example: js const btn = document.querySelector("button"); function random(number) { return Math.floor(Math.random() * (number + 1)); } btn.addEventListener("click", () => { const rndCol = `rgb(${random(255)} ${random(255)} ${random(255)})`; document.body.style.backgroundColor = rndCol; }); The HTML js function bgChange() { const rndCol = `rgb(${random(255)} ${random(255)} ${random(255)})`; document.body.style.backgroundColor = rndCol; } The earliest method of registering event handlers found on the Web involved event handler HTML attributes (or inline event handlers) like the one shown above -- the attribute value contains the JavaScript code you want to run when the event occurs. The above example invokes a function defined inside a