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?

3
  • See stackoverflow.com/questions/34516880/… Commented Dec 30, 2015 at 8: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 Commented Dec 30, 2015 at 8:09
  • 1
    looks like it is duplicate question after all and the best solution is: stackoverflow.com/questions/8729319/… Commented Dec 30, 2015 at 8:23

2 Answers 2

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.

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

Comments

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.

2 Comments

thank you I understand that, so how can I js submit form with input named submit?
How send a form with Javascript when input name is "submit"?. Or in short: Don't have an element named submit.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.