Using JavaScript To Run Form Events

Creating a form as part of an AJAX control is a common practice, but when you include a submit button within the form the default behaviour of the form is to redirect to the destination supplied in action. To run a form using JavaScript you need to include two things.

In order to overwrite the normal operation of a form you will need to replace the action attribute with some JavaScript code that returns false. This is can be done in the form of come in-line JavaScript code (commonly called a bookmarklet) in the place of any URL in the action attribute of the form.

<form action="javascript:return false;" method="post" id="analysisForm" >

Next, in order to actually get JavaScript to intercept the form submission action an onsubmit event is added to the form tag.

<form action="javascript:return false;" method="post" id="analysisForm" onsubmit="return runForm();" >

This will allow you to have a function in place to action whatever it is that the form will do. In the example above the function runForm() is run.

Share:

  • Add news feed
  • Bookmark this on Delicious

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h2> <h3> <h4> <h5> <h6> <pre> <span> <p> <br />
  • Syntax highlight code surrounded by the {syntaxhighlighter SPEC}...{/syntaxhighlighter} tags, where SPEC is a Syntaxhighlighter options string or "class="OPTIONS" title="the title".

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.