Open In App

HTML DOM onsubmit Event

The onsubmit event in HTML DOM occurs after the submission of a form. The form tag supports this event.

Syntax: 



<element onsubmit="Script">
object.onsubmit = function(){myScript};
object.addEventListener("submit", myScript);

Example: In this example, we will see the onsubmit event using Javascript. 




<center>
    <h1 style="color:green">GeeksforGeeks</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.




<center>
    <h1 style="color:green">GeeksforGeeks</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: 

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.


Article Tags :