Guy
Guy

Reputation: 50

Code not working (JavaScript)

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>

Upvotes: -1

Views: 86

Answers (3)

Zee
Zee

Reputation: 8488

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>

Upvotes: 2

Srinath Mandava
Srinath Mandava

Reputation: 3462

<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

Upvotes: 5

Dhaval
Dhaval

Reputation: 2379

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

Upvotes: -1

Related Questions