John Doe
John Doe

Reputation: 31

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

Why doesn't the following code work?

<html>
<body>
<form id="myForm" action="http://example.com" method="POST">
<input type="text" name="engine" value="v2.5" />
<input type="text" name="verify" value="2" />
<input type="text" name="submit" value="Save" />
</form>
<script type="text/javascript">
document.getElementById("myForm").submit();
</script>
</body>
</html>

I'm getting the following error:

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

Thanks.

I want to send a POST parameter named submit through forms, how can this be done automatically?

Upvotes: 3

Views: 19437

Answers (4)

Sabri CH
Sabri CH

Reputation: 1

go to submit button and check if you put id = submit EX: if your id was submit you should change it to other id that's all

Upvotes: -1

lawrence Da
lawrence Da

Reputation: 121

Since you have an input with the name submit, try to use following line:

document.getElementById("myForm").submit.click();

Upvotes: 7

Scott
Scott

Reputation: 5379

Rename <input type="text" name="submit" value="Save" /> to something other than submit, and it should work.

This is because you can access form values using object notation in Javascript, so by calling submit(), you are attempting to execute the submit field as a function.

However, your question is how to send a POST parameter named submit. You might want to take a look at XMLHttpRequest, like this:

xmlhttp.open("POST", "http://example.com", true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("engine=v2.5&verify=2&submit=Save");

Upvotes: 5

Nathan Piercy
Nathan Piercy

Reputation: 358

myForm.submit is a reference to the text input with name="submit":

<input type="text" name="submit" value="Save" />

Change the name to something other than "submit".

Upvotes: 18

Related Questions