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.

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

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.

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.

Add new comment

The content of this field is kept private and will not be shown publicly.
5 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.