CSS Portal

HTML onsubmit Event Attribute

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
onsubmit
HTML Event AttributeThis event attribute is supported by all modern browsers.
Updated 14th October 2023

Description

The onsubmit HTML event attribute is used to specify a JavaScript function that is executed when a form is submitted. The function can be used to perform any action, such as validating the form data, sending the data to a server, or displaying a confirmation message to the user.

To use the onsubmit attribute, simply add it to the <form> element and assign it the name of the JavaScript function that you want to be executed when the form is submitted. For example, the following code would execute the submitForm() function when the user clicks the submit button:

<form onsubmit="submitForm()">
  <input type="text" name="name">
  <input type="submit" value="Submit">
</form>

The submitForm() function could then be used to validate the form data, send the data to a server, or display a confirmation message to the user. For example, the following code would validate the form data and then send it to a server:

function submitForm() {
  // Validate the form data.

  // Send the form data to a server.
}

The onsubmit event attribute is a powerful way to control the behavior of forms. It can be used to perform a wide variety of tasks, such as validating form data, sending data to a server, or displaying messages to the user.

Syntax

<element onsubmit="script">

Values

  • scriptThe name of the script to use when the event has been triggered.

Example

<!DOCTYPE html> 
<html>
<head>
<title>onsubmit event</title>
</head>
<body>
<form action="#" onsubmit="testFunction()" >
Email us something: <input type="text" name="mail">
<input type="submit" value="Submit">
</form>
<script>
function testFunction() {
alert ( "EXAMPLE: The form has been submitted!" );
}
</script>
</body>
</html>
Open in editor

Browser Support

The following information will show you the current browser support for the HTML onsubmit event attribute. Hover over a browser icon to see the version that first introduced support for this HTML event attribute.

This event attribute is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee