O'Reilly logo

JavaScript Recipes: A Problem-Solution Approach by Keith Cirkel, Russ Ferguson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

© Russ Ferguson and Keith Cirkel 2017
Russ Ferguson and Keith CirkelJavaScript Recipes10.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

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required