HTML onsubmit Event Attribute
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>
Browser Support
The following table will show you the current browser support for the HTML onsubmit
Event Attribute.
Desktop | |||||
Yes | Yes | Yes | Yes | Yes |
Tablets / Mobile | |||||
Yes | Yes | Yes | Yes | Yes | Yes |
Last updated by CSSPortal on: 14th October 2023