JavaScript onsubmit Event

You are Here:

JavaScript onsubmit Event

The onsubmit event occurs when the user submits a form.

onsubmit Event as Attribute

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <p>Type your name and click 'Submit'.</p> <form action="/name.php" onsubmit="myFunction()" method="get"> <input type="text" name="myName"> <input type="submit" value="Submit"> </form> <script> function myFunction() { alert("I'm going to submit a form"); } </script> </body> </html>

Syntax

As Attribute

<element onsubmit = "JavaScript">

As Property

object.onsubmit = function(){ // code };

Using Event Listener

object.addEventListener("submit" , myScript);

onsubmit Event as Property

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <p>Type your name and click 'Submit'.</p> <form action="/name.php" method="get"> <input type="text" name="myName"> <input type="submit" value="Submit"> </form> <script> var x = document.getElementsByTagName("form")[0]; function myFunction() { alert("I'm going to submit a form"); } x.onsubmit = myFunction; </script> </body> </html>

Using addEventListener() Method

Note: The addEventListener() method is not supported in Internet Explorer 8 and below.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <p>Type your name and click 'Submit'.</p> <form action="/name.php" method="get"> <input type="text" name="myName"> <input type="submit" value="Submit"> </form> <script> var x = document.getElementsByTagName("form")[0]; function myFunction() { alert("I'm going to submit a form"); } x.addEventListener("submit", myFunction); </script> </body> </html>

Reminder

Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in JavaScript.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author