submit | onsubmit event

Fires when a FORM is about to be submitted.

HTML 4.01 Specification, Section 18.2.3

Syntax

HTML Attribute <element onsubmit = "handler(event)">
Event Property object.onsubmit = handler;
attachEvent Method object.attachEvent("onsubmit", handler)
addEventListener Method object.addEventListener("submit", handler, useCapture)

Event information

SynchronousNo
BubblesNo
CancelableYes

Event handler parameters

pEvtObj [in]

Type: IHTMLEventObj

Standards information

Remarks

You can override this event by returning false in the event handler. Use this capability to validate data on the client side to prevent invalid data from being submitted to the server. If the event handler is called by the onsubmit attribute of the form object, the code must explicitly request the return value using the return function, and the event handler must provide an explicit return value for each possible code path in the event handler function.

The submit method does not invoke the onsubmit event handler.

Causes a form to be sent to whatever location is stipulated in the ACTION attribute of the form object.

To invoke this event, do one of the following:

The pEvtObj parameter is required for the following interfaces:

Examples

This example shows how to use onsubmit on a form. You can return false to prevent the form from being submitted. Note the use of the return keyword in the onsubmit event handler.

Code example: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/onsubmit.htm


<script type="text/javascript">
    
    function displayAnswer ()
    {
        var question = document.getElementById('question');
        var selected = -1;
        for (var i=0; i<question.answer.length; i++)
        {
            if (question.answer[i].checked) {
                selected = i;
                break;
            }
        }
        if (selected > -1) {
              alert("You answered:  " + question.answer[selected].value );
              return true;
        }

        alert("Please select an answer.");
        return false;
      }
    
</script>




<form id="question" action="#" name="question" onsubmit="return(displayAnswer())">
    <p style="font-size: xx-large">Are you a turtle?</p>
    <p><label><input name="answer" type="radio" value="YES" />YES</label></p>
    <p><label><input name="answer" type="radio" value="NO" />NO</label></p>
    <p><label><input name="answer" type="radio" value="MAYBE" />MAYBE</label></p>
    <p><input type="submit" value="SUBMIT" />&nbsp;&nbsp;
    <input type="reset" value="RESET" /></p>
</form>


See also

form
Reference
action
method

 

 

Show:
© 2014 Microsoft. All rights reserved.