1

I have the following form. When I have the input name set to submit, it won't submit the form, why is that?

<form id="login" method="post" action="">
    <input type="hidden" name="username" value="someval">
    <input type="hidden" name="userpass" value="somepass">
    <input type="hidden" name="submit" value="Login">
</form>
<script type="text/javascript">
  var timeout = 12;
  setTimeout(function () {
      document.getElementById('login').submit();
  }, timeout);
</script>

Error message is:

Uncaught TypeError: document.getElementById(...).submit is not a function(…)

I guess the submit name is erasing the submit function in some way?

JSFiddle with and without submit name.

edit to my question:

How can I submit the form using js when it includes input named submit?

talsibony
  • 8,448
  • 6
  • 47
  • 46
  • See http://stackoverflow.com/questions/34516880/javascript-errors-reference-what-does-this-error-mean – Michał Perłakowski Dec 30 '15 at 08:02
  • as I thought the submit name erase for some reason the submit function because when I document.getElementById('login').userpass it gives me the userpass input element same as with the submit – talsibony Dec 30 '15 at 08:09
  • 1
    looks like it is duplicate question after all and the best solution is: http://stackoverflow.com/questions/8729319/how-send-a-form-with-javascript-when-input-name-is-submit#answer-34525540 – talsibony Dec 30 '15 at 08:23

2 Answers2

1

JavaScript has a single namespace for properties and methods. Each named field becomes a property of the form object. In your case, document.getElementById('login').submit is no longer a method, but an input field.

Sergey Salnikov
  • 321
  • 1
  • 5
1

Because you may access child elements of the form as direct properties. So in your case:

'use strict';
let form = document.getElementById('login');
console.log(form.username, form.userpass, form.submit); // 3 input elements

So when you have an element named submit, it shadows the submit() function, and thus you get the error about it not being a function.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • thank you I understand that, so how can I js submit form with input named submit? – talsibony Dec 30 '15 at 08:11
  • [How send a form with Javascript when input name is "submit"?](http://stackoverflow.com/q/8729319). Or in short: Don't have an element named submit. – Madara's Ghost Dec 30 '15 at 08:13