HTML DOM onsubmit Event
The onsubmit event in HTML DOM occurs after the submission of a form. The form tag supports this event.
Syntax:
- In HTML:
<element onsubmit="Script">
- In JavaScript:
object.onsubmit = function(){myScript};
- In JavaScript, using the addEventListener() method:
object.addEventListener("submit", myScript);
Example: In this example, we will see the onsubmit event using Javascript.
html
< center > < h1 style = "color:green" >w3wiki</ h1 > < h2 >HTML DOM onsubmit Event</ h2 > < form id = "formID" action = "#" > Enter name: < input type = "text" name = "fname" > < input type = "submit" value = "Submit" > </ form > </ center > < script > document.getElementById("formID").onsubmit = function() {GFGfun()}; function GFGfun() { alert("form submitted"); } </ script > |
Output:
Example: In this example, we will see the onsubmit event using the addEventListener() method in Javascript.
html
< center > < h1 style = "color:green" >w3wiki</ h1 > < h2 >HTML DOM onsubmit Event</ h2 > < form id = "formID" action = "#" > Enter name: < input type = "text" name = "fname" > < input type = "submit" value = "Submit" > </ form > </ center > < script > document.getElementById( "formID").addEventListener("submit", GFGfun); function GFGfun() { alert("form submitted"); } </ script > |
Output:
We have a complete list of HTML DOM methods, to check those please go through this HTML DOM Object Complete reference article.
Supported HTML Tags: HTML <form> tag
Supported Browsers: The browsers supported by HTML DOM onsubmit Event are listed below:
- Google Chrome
- Internet Explorer
- Firefox
- Apple Safari
- Opera
We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.