Did you find this useful?
Socialize it today.


HTML Event Attributes

Thursday, March 01, 2012

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 conjuntion with the <body> element so that a script can run when the page loads.  Here are some examples.



Syntax: <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>.


Mouse Events

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


Keyboard Events

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> elements


EventValueDescription
onabortscriptScript to run when loading of an image is interrupted

Please help us spread the word by socializing it today!

email contact us

Did you find something wrong with the information on this page? Please take a moment to report it to us so that we can continue to improve the quality of the information on this site. Click here to report an issue with this page.



Recommended Books & Training Resources

HTML CSS and JavaScript Editor HTML CSS: The Complete Reference Creating Web Pages Simplified