Javascript DOM onsubmit Event via addEventListener() method

Introduction

In JavaScript, using the addEventListener() method:

object.addEventListener("submit",
       myScript);

This example uses the addEventListener() method to attach a "submit" event to a form element.

When you submit the form, a function is triggered which alerts some text.

View in separate window

<!DOCTYPE html>
<html>
<body>
<form id="myForm" action="/action_page.php">
  Enter name: <input type="text" name="fname">
  <input type="submit" value="Submit">
</form>/*from ww  w  .  ja  v  a2 s.co  m*/

<p id="demo"></p>
<script>
document.getElementById("myForm").addEventListener("submit", myFunction);

function myFunction() {
  document.getElementById("demo").innerHTML = "The form was submitted";
}
</script>

</body>
</html>
Bubbles: Yes
Cancelable: Yes
Event type: Event
Supported HTML tags: <form>



PreviousNext

Related