Fancy Event Handling

Dynamic web pages call for dynamic reactions to user actions. We've discussed how to write JavaScript that reacts when users click links, buttons, and form elements. Now it's time to learn about more complicated event handling: how to accurately read the keyboard and the mouse.

The event Object

Whenever an event occurs, an event object is generated. The nature of this object depends on the event which generated it. To access the event object, simply pass the keyword event to whichever function is handling the event:

<a href = "#" onClick = "handleClick(event); return false; ">Click me!</a>.

The event object is most frequently accessed when you want to know which key a user has pressed, or precisely where the mouse is.

Keyboard ...

Get The Book of JavaScript, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.