Class 5 – How to Cancel the Default Behavior of Links and Forms

March 14th, 2009 § 0

Clicking a link and submitting a form will, by default, take you to another page.  In some cases, you may want to override that behavior.

The following example shows how to prevent both links and forms from taking the user away from the current page:

http://onepotcooking.com/amosbloomberg/spring2010/class5/cancel_links_and_forms/cancel_links_and_forms.html

In this example, we use the <a> tag’s “onclick” attribute and the <form> tag’s “onsubmit” attribute to handle user click and submit events, respectively.  By telling the code to “return false;” when a user clicks a link or submits a form, we tell the browser to prevent that element from performing its default behavior.

Related posts:

  1. Class 5 – Detecting Form Element Values and Canceling the Default Behavior of Forms and Links

Tagged:

§ Leave a Reply

What's this?

You are currently reading Class 5 – How to Cancel the Default Behavior of Links and Forms at Web Development Intensive.

meta