Computers & ProgrammingHTML/XHTMLWeb Development

HTML Event Attributes

The W3C has defined a set of core events that are associated with nearly every HTML and/or XHTML element.

Event attributes are an important part of learning HTML so being familiar with them is a good idea. Most of these events cover simple user interaction, such as the click of a mouse button or a key being pressed.

A few elements, such as form controls, have some special events associated with them. Another useful event is onload which can be used in conjunction with the <body> element so that a script can run when the page loads.

Here is an example:

<startTag event="script">Element Content</endTag>

Mouse and Keyboard Events

Mouse and keyboard events can be used when you want a particular script to be run when the mouse or keyboard interacts with an element.

The following events can be used in all elements except <base>, <bdo>, <br>, <frame>, <frameset>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, and <title>.

EventValueDescription
onclickscriptScript to run on a mouse click
ondblclickscriptScript to run on a mouse double-click
onmousedownscriptScript to run when mouse button is pressed
onmousemovescriptScript to run when mouse pointer moves
onmouseoutscriptScript to run when mouse pointer moves out of an element
onmouseoverscriptScript to run when mouse pointer moves over an element
onmouseupscriptScript to run when mouse button is released
EventValueDescription
onkeydownscriptScript to be run when a key is pressed
onkeypressscriptScript to be run when a key is pressed and released
onkeyupscriptScript to be run when a key is released

Form Events

Form events are also very common and useful. The following events can be used in <form> elements:

EventValueDescription
onblurscriptScript to run when an element loses focus
onchangescriptScript to run when an element changes
onfocusscriptScript to run when an element gets focus
onresetscriptScript to run when a form is reset
onselectscriptScript to run when an element is selected
onsubmitscriptScript to run when a form is submitted

Body and Frameset Events

There are two events that can be used with the <body> and <frameset> elements:

EventValueDescription
onloadscriptScript to run when a document loads
onunloadscriptScript to run when a document unloads

Image Event

There is only one event that can be used with the <img> element:

EventValueDescription
onabortscriptScript to run when loading of an image is interrupted

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top