4

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.

Hassan Imam
22.6k6 gold badges45 silver badges53 bronze badges
asked Jul 8, 2017 at 17:48
0

1 Answer 1

10

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);
}
answered Jul 8, 2017 at 17:55
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.