HTML and CSS Reference
however, it is whether or not a handler is assigned that determines whether or not you can
run your own code when the event is triggered.
This objective covers how to:
■ Use events, including handling an event by using an anonymous function and
declaring and handling bubbled events
■ Handle DOM events, including OnBlur, OnFocus, and OnClick
■ Create custom events
The reason an API provides events is so that developers can inject their own processing amid
the DOM. This section discusses the ability to hook up to these events.
The idea of hooking up an event is to tell the browser that when a certain event occurs,
it should call a specified function. The function assigned to an event is said to be an event
listener listening for that event. The need, then, is to assign a function to an event to listen for
when that event occurs.
You can hook up an event in three ways:
■ Declare it directly in the HTML markup.
■ Use the newer add and remove methods on the element object to associate event
named function and assign an anonymous function. The difference between these two will be
To get started, you need to understand the concept of a single object common to all DOM
event handlers, and that's the event object itself.
In general, the event object is a common object available within event handlers that provides
metadata about the event. For example, if keyboard events are being handled, you might
want to know which key was pressed. If mouse events are being handled, you might want to
know which mouse button was pressed. The event object contains all these properties.