spacer
Yehuda Shiran May 1, 2000
Form Cancellation
Tips: May 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

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

A form can be submitted via the Enter key when it consists of only one text field, and the cursor is currently located in that field. Sometimes, we want to force an action by the Enter key, without submitting the form. The trick is to use the onSubmit event handler, do the required action when the trigger goes off, and then cancel the event by returning a false value. In this way, we still utilize the advantages of the submit button (submission via the Enter key) without actually submitting the form.

Let's take an example. Suppose a form's header is defined as:

<FORM NAME="organizer", onSubmit="display(); return false;">

and is composed of two elements:

  • A text field, NAME="prefix"
  • A submit button, VALUE="Display"

The user can submit the form by clicking the button labeled "Display," or by pressing Enter when the cursor is located in the text field. When the user submits the form, the onSubmit event handler is triggered. It invokes the display() function.

The onSubmit event handler returns false in order to cancel the form's submission. If we aren't really submitting the form, then why didn't we use a standard button instead of a submit button? The answer is simple. If we used a standard button to invoke display() via its onClick event handler, that would be the only way to call display(). By using the form's onSubmit event handler to call the function, the user can also load the desired page by pressing Enter within the text field.

Learn more about canceling a form submission in Column 15, Lookup Lists.


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