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, ...