|
Helping ordinary people create extraordinary websites! |
JavaScript EventsUsers interacting with HTML tags can be linked to JavaScript very easily, through events. Basic events in HTMLMost HTML tags can be interacted with by the user. At the simplest example, a button can be clicked on. However, something as simple as a paragraph tag can be clicked on. Events are named and are assigned via a HTML tag attribute; names usually start with "on" and are fairly descriptive, e.g. "onclick", "onmouseover". These event names all correspond to HTML tag attributes:
Here, the Binding events with DOM scriptingPlease read our JavaScript DOM scripting guide before proceeding. We have previously altered an attribute of a HTML node on-the-fly:
We can easily do the same for events: <p id="someid">Hello World!</p> Try this out in a web browser. When you click on the "Hello World!" text, a popup alert box will appear saying "Hello!". We call our earlier approach, of including the events in the actual HTML, inline events or inline DHTML (DHTML = dynamic HTML). We call this approach binding the events, as they are attached to the HTML nodes on the fly as the browser interprets the JavaScript. This is also by far the preferred method, as it is more reliable, and your events can all be bound in one place, allowing for better maintainability.
|
|