Run JavaScript function on form submit

14,974

Here you go with the solution https://jsfiddle.net/aw27toyv/4/

**HTML**
<form onsubmit="return false;" method="post" name="myForm">
    <label>Name</label>
    <input type="text" name="name" id="name" />
    <input type="submit" name="submit" onclick="validate()" />
</form>

**JS**
validate = function() {
    var name = document.getElementById("name").value;
    alert("Your name is " +name);
}
Share:
14,974
Greggo
Author by

Greggo

Updated on December 02, 2022

Comments

  • Greggo
    Greggo over 1 year

    Trying to get back into Javascript, having a little trouble with this very basic thing.

    https://jsfiddle.net/gfitzpatrick2/aw27toyv/3/

    var name = document.getElementById("name");
    function validate() {
      alert("Your name is " +name);
    }
    <form action="" method="post" name="myForm">
      <label>Name</label>
      <input type="text" name="name" id="name" />
      <input type="submit" name="submit" onclick="validate()" />
    </form>

    Just wanted a pop up box showing the name you have entered in the field, not sure why it's not working. Am I way off?

    Thanks.