What Triggers onsubmit?

What Triggers onsubmit? thumbnail
Onsubmit is an attribute of an HTML form.

In Hypertext Markup Language (HTML) -- a language used to tag the elements of a hypertext document, for publishing on the World Wide Web -- the onsubmit attribute is associated with an element, or object, known as a form. Onsubmit is triggered by a user clicking on the “Submit” button, and calls a script, known as a “handler,” when that event occurs. The onsubmit event is supported in all major Web browsers, including Internet Explorer, Firefox, Opera, Google Chrome and Safari.

  1. HTML Forms

    • HTML forms are used on the Web to collect user input, and are created using the "<form>" and "</form>" tags. HTML typically contain special elements -- text fields, checkboxes, radio buttons and menus -- called controls, which users use to complete forms before submitting them. The expected behavior of HTML forms is defined by the HTML specification published by the World Wide Web Consortium.

    Validation

    • The onsubmit attribute allows validation to be performed on the control elements of HTML forms before they’re submitted. The user completes the form, clicks the “Submit” button and onsubmit triggers the onsubmit handler, which runs a validation routine. If the contents of the form are valid, the validation routine returns “true” and the contents of the form are submitted to the Web address specified in the form action. If the contents are invalid, the validation routine returns “false,” the submission is cancelled and the user remains on the current page.

    Syntax

    • The general syntax of the onSubmit attribute is <ELEMENT onsubmit = "handler(event);" >, where the “handler(event)” portion is required. The syntax onsubmit = “JavaScriptCode”, for example, specifies that a script file, written in the JavaScript scripting language, is executed when the user clicks the “Submit” button.

    Default Event Handler

    • Onsubmit has no default event handler, so it is the responsibility of the Web designer to specify a handler and an unambiguous return value from that handler. The event handler could be a “simplealert” statement, which displays an alert window with a title and a text body, or include calls to one or more sophisticated functions. Onsubmit may behave unexpectedly if no value at all is returned from the event handler. The contents of a form may or may not be submitted -- depending on the Web browser -- which introduces the possibly of incomplete, or invalid, data being submitted to a Web server. Web designers can avoid this problem by explicitly returning “false” from the event handler.

Related Searches:

References

  • Photo Credit Jason Reed/Stockbyte/Getty Images

Comments

Related Ads

Featured