spacer
Yehuda Shiran February 2, 2000
Event Handler Definition
Tips: February 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

Internet Explorer 4.0x and up supports the two most basic event handler methods:
  • HTML attribute
  • Property assignment

Consider the following form definition:

<FORM NAME="myForm">
<INPUT TYPE="button" NAME="myButton" VALUE="click here">
</FORM>

When you click the button, nothing happens because no script or function is attached to its click event. For instance, you could associate it with a function that generates an alert dialog box by placing an onClick event handler script in the body of the tag:

<SCRIPT LANGUAGE="JavaScript">
<!--

function myAlert() {
  alert("Thank you for clicking the button.");
}

// -->
</SCRIPT>

<FORM NAME="myForm">
<INPUT TYPE="button" NAME="myButton" 
       VALUE="click here" onClick="myAlert()">
</FORM>

Another way to associate a function with a specific event is to assign its reference to the object's event handler property:

<SCRIPT LANGUAGE="JavaScript">
<!--

function myAlert() {
  alert("Thank you for clicking the button.");
}

// -->
</SCRIPT>

<FORM NAME="myForm">
<INPUT TYPE="button" NAME="myButton" VALUE="click here">
</FORM>

<SCRIPT LANGUAGE="JavaScript">
<!--

document.myForm.myButton.onclick = myAlert;

// -->
</SCRIPT>

And yet another variation on the same theme is defining the event handler function in-line. We chose a different event handler to demonstrate the usage of this:

<FORM NAME="myForm">
<INPUT TYPE="button" NAME="myButton" VALUE="click here">
</FORM>

<SCRIPT LANGUAGE="JavaScript">
<!--

document.myForm.myButton.onclick = function() {
  this.style.border = "2px buttonhighlight inset";
}

// -->
</SCRIPT>

Learn more about event handling in The Internet Explorer Event Model.


People who read this tip also read these tips:

Look for similar tips by subject:

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business