Event handlers have had browser support since Internet Explorer version 5, Netscape version 6, and Firefox version 1.
Note: Browsers or firewalls may block event handlers. If the examples on this page do not work, that is the first thing to suspect.
|Page Related||onload onunload|
|Mouse Related||onclick ondblclick onmouseover onmouseout onmousedown onmousemove onmouseup onselect|
|Keyboard Related||onkeydown onkeyup|
|Related to Forms||onfocus onblur onchange onsubmit|
Event handlers are considered to be attributes, but they do not have values, as most attributes do. Instead, the event handler is set equal to a statement of what is to happen when the event occurs. We will call this the event statement.
The event statement assigns a new value to an html DOM property. When the event occurs, the html DOM property will change to the new value, thus changing the web page. This is called Dynamic Html.
For our first example, we will use mouse clicks to change an image.
The image is changed by changing the source file. The code is:
<img src="hackanm.gif" width="12%" onclick="src='boy.gif'" ondblclick="src='hackanm.gif'">
Note the use of single quotes within the event statements.
This example is deceptively simple, because "src" is an attribute of <img>, and it appears that the event statement "src='boy.gif'" is simply resetting the attribute. This is not true. It works only because "src" also happens to be the DOM name for the src attribute.
<p onclick="innerHTML='Something new to say'"> <b>Click the mouse on this text to see what happens</b> </p>
Click the mouse on this text to see what happens.
<p style="color:blue "onmouseover="style.color='red'" onClick="style.color='green'"> <b>Move the mouse over this text. Click it.</b></p>
Move the mouse over this text. Click it.This example makes clear the distinction between the attribute statement setting the original color of the paragraph, and the event statements. When "style" is used as an attribute, the correct format is:
style="color:blue"When "style" is used as a DOM name the correct format is:
style.color='red' style.color='green'This example also shows that more than one event handler can be used simultaneously.
<p onclick="style.visibility='hidden'" onmouseout="style.visibility='visible'"> <big>Click the mouse on this text to see what happens</big></p>
Click the mouse on this text to see what happensHowever the reverse will not work. Invisible text can be made visible, but not by reversing this process. The invisible text below will not become visible with a click, a mouseover or a mouseout.
<p style="visibility:hidden" onclick="style.visibility='visible'"> onmouseover="style.visibility='visible'" onmouseout="style.visibility='visible'" <big>This invisible text is supposed to become visible, but it doesn't.</big> </p>
<p style="text-transform:lowercase" onmouseover="style.textTransform='uppercase'" onmouseout="style.color='blue'"> <b>Move the mouse over this text to see what happens</b></p>
Move the mouse over this text to see what happens
<p onmouseover="style.fontSize='40px'; style.lineHeight='40px'" onmouseout="style.color='blue'"> <b>Mouse over this text to see what happens</b></p>
Mouse over this text to see what happens
<p onclick="window.alert('False alarm')"> Click on this Text for a Big Surprise! </p>
Click on this Text for a Big Surprise
<p onclick="window.document.write('Another False alarm')"> Click on this Text </p>
Click on this Text
<p onclick="window.print()"> Print this page. </p>
Print this page.
Copyright January 2005, 2006 Nancy E. Knox