Using JavaScript To Run Form Events

13 March, 2008 | JavaScript

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.

Write a comment