8. Event Handling

Handling events is one of the fundamental uses of JavaScript. Loading a Web page, moving the cursor, entering text into a textarea, submitting a form: these are all events that occur within the browser to which JavaScript can respond. Out of necessity, Chapter 2, JavaScript in Action, introduced the very basics of event handling, and it also presented two events, used in most of the book’s examples thus far. In this chapter, you’ll learn everything you need to know to handle the myriad of events in JavaScript.

The Premise of Event Handling

Chapter 2 explained that creating an event handler in JavaScript is a matter of associating an event and an object with a JavaScript function. For example:

window.onload = init;

That one line ...

Get Modern JavaScript: Develop and Design 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.