© Russ Ferguson and Keith Cirkel 2017

Russ Ferguson and Keith Cirkel, JavaScript Recipes, 10.1007/978-1-4302-6107-0_18

18. Working with Events

Russ Ferguson and Keith Cirkel2

(1)Ocean, New Jersey, USA

(2)London, UK

How Do Events Work in JavaScript?

Problem

What are DOM events and how do you handle them?

Solution

DOM events notify you when something is happening in the browser, for example a mouse click. There is a long list of events that you as a developer can listen for. A function is then called to handle the event once it occurs.

The Code

Listing 18-1. Listening for the DOMContentLoaded Event
function loadedFunction(){         console.log('The DOM has been loaded')  //returns The DOM has been loaded}document.addEventListener('DOMContentLoaded', loadedFunction, ...

Get JavaScript Recipes: A Problem-Solution Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.