Run ❯
Get your
own
website
Result Size:
625 x 565
×
Change Orientation
Save Code
Change Theme, Dark/Light
Go to Spaces
<!DOCTYPE html> <html> <body> <p>Use the addEventListener() method to attach a "submit" event to a form element.</p> <p>When you submit the form, a function is triggered which alerts some text.</p> <form id="myForm" action="/action_page.php"> Enter name: <input type="text" name="fname"> <input type="submit" value="Submit"> </form> <script> document.getElementById("myForm").addEventListener("submit", myFunction); function myFunction() { alert("The form was submitted"); } </script> </body> </html>