Greggo
Greggo

Reputation: 79

Run JavaScript function on form submit

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.

Upvotes: 4

Views: 20615

Answers (1)

Shiladitya
Shiladitya

Reputation: 12181

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);
}

Upvotes: 10

Related Questions