HTML and CSS Reference
In-Depth Information
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
Using events
The reason an API provides events is so that developers can inject their own processing amid
all the action taking place in a program. JavaScript enables you to do exactly this throughout
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.
Assign the function to the event property of the element object through JavaScript.
Use the newer add and remove methods on the element object to associate event
handlers.
When assigning event handlers through JavaScript, you have two choices: provide a
named function and assign an anonymous function. The difference between these two will be
examined.
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.
Event objects
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.
 
 
Search WWH ::




Custom Search