-1

I am making this program where the value in the text-box will be alerted. I know this is hardly anything, but already something isn't working. I checked the developer tools console panel and it didn't show any errors.

<!DOCYTPE html>
<html>
<head>
  <script type="text/javascript">
    var submit = function() {
      var name = document.getElementById('name').value;
      alert(name);
    }
  </script>
</head>
<body>
  <form>
    <input type="text" id='name'>
    <button type="button" onclick="submit()">Submit</button>
  </form>
</body>
</html>

1
  • Have you tried declaring the submit function as this: function submit() and not as an anonymous function? Commented Jun 5, 2015 at 8:53

3 Answers 3

5

<html>
<head>
  <script type="text/javascript">
    var submit1 = function() {
      var name = document.getElementById('name').value;
      alert(name);
    }
  </script>
</head>
<body>
  <form>
    <input type="text" id='name'>
    <button type="button" onclick="submit1()">Submit</button>
  </form>
</body>
</html>

That is because there's already be a function named submit(). Change it to submit1() or any other name . It'll work

Sign up to request clarification or add additional context in comments.

1 Comment

This is the correct answer, although the explanation is a bit shaky.
2

submit() is a predefined method in JavaScript that is used to submit the form.

So you can't use it like that. You need to change the function name to something else.

<!DOCYTPE html>
<html>
<head>
  <script type="text/javascript">
    var submitForm = function() {
      var name = document.getElementById('name').value;
      alert(name);
    }
  </script>
</head>
<body>
  <form>
    <input type="text" id='name'>
    <button type="button" onclick="submitForm()">Submit</button>
  </form>
</body>
</html>

Comments

-1

var test= function() {
      var name = document.getElementById('name').value;
      alert(name);
    }
<input type="text" id='name'>
    <button type="button" onclick="test()">Submit</button>

Comments

Your Answer

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.