320

Can anyone tell me what is going wrong with this code? I tried to submit a form with JavaScript, but an error ".submit is not a function" shown. See below for more details of the code:

<form action="product.php" method="get" name="frmProduct" id="frmProduct" enctype="multipart/form-data">

<input onclick="submitAction()" id="submit_value" type="button" name="submit_value" value="">

</form>

<script type="text/javascript">
    function submitAction()
    {
        document.frmProduct.submit();
    }
</script>

I also tried this:

<script type="text/javascript">
    function submitAction()
    {
        document.forms["frmProduct"].submit();
    }
</script>

Both show me the same error :(

trejder
  • 17,148
  • 27
  • 124
  • 216
Jin Yong
  • 42,698
  • 72
  • 141
  • 187

19 Answers19

895

submit is not a function

means that you named your submit button or some other element submit. Rename the button to btnSubmit and your call will magically work.

When you name the button submit, you override the submit() function on the form.

Stefan van den Akker
  • 6,661
  • 7
  • 48
  • 63
epascarello
  • 204,599
  • 20
  • 195
  • 236
  • 1
    Just ran into this problem with selenium. "submit" seems to be the standard name for submit buttons in typo3 frontend logins. >. – Paul Voss Jun 21 '12 at 16:21
  • 68
    Here's a handy trick. If you're stuck with your submit button being `#submit`, you can get around it by stealing another form instance's `submit()` method, eg: `document.createElement('form').submit.call(document.frmProduct)`. – Neil Apr 21 '13 at 07:23
  • 41
    Seems everyone has to learn this one the hard way. Would make a good job interview question – SeanDowney Aug 19 '13 at 19:04
  • 9
    I simply removed the name='submit' and Bob's your Aunty it worked!! – zzapper Jul 21 '14 at 13:41
  • 6
    I just wasted my 2-3 hours for this issue, thanks a lot for the answer – Mehul Prajapati May 19 '17 at 20:56
  • 2
    In case it helps anyone else: I had an `id='submit'` on the button that was throwing things off (removing/renaming the name attribute was not sufficient). – kayge May 29 '18 at 20:09
  • 1
    @kayge Just had this issue and had to update the id as well. – nwolybug Sep 06 '18 at 13:25
  • 8
    JavaScript really is the worst ever – Ben Harold Jun 10 '19 at 21:50
  • @NeilE.Pearson Can you explain with other words so hopeful I understand. You mean rewrite `submit()` to another name and then use it? – Timo May 13 '21 at 19:22
  • 1
    @Timo no. It's a way to access the prototypical `submit` function that's been made inaccessible by overriding it with something else. – Neil May 19 '21 at 22:56
  • "When you name the button submit, you override the submit() function on the form." - this is the thing! Thank you – Naderio Jul 16 '21 at 20:31
23

Make sure that there is no another form with the same name and make sure that there is no name="submit" or id="submit" in the form.

Xiddoc
  • 3,369
  • 3
  • 11
  • 37
gopeca
  • 1,601
  • 12
  • 12
18

If you have no opportunity to change name="submit" you can also submit form this way:

function submitForm(form) {
    const submitFormFunction = Object.getPrototypeOf(form).submit;
    submitFormFunction.call(form);
}
Terbiy
  • 576
  • 5
  • 14
14
<form action="product.php" method="post" name="frmProduct" id="frmProduct" enctype="multipart/form-data">

<input id="submit_value" type="button" name="submit_value" value="">

</form>

<script type="text/javascript">

document.getElementById("submit_value").onclick = submitAction;

function submitAction()
{
    document.getElementById("frmProduct").submit();
    return false;
}
</script>

EDIT: I accidentally swapped the id's around

Chad Grant
  • 44,326
  • 9
  • 65
  • 80
6

I had the same issue when i was creating a MVC application using with master pages. Tried looking for element with 'submit' as names as mentioned above but it wasn't the case.

For my case it created multiple tags on my page so there were some issues referencing the correct form.

To work around this i'll let the button handle which form object to use:

onclick="return SubmitForm(this.form)"

and with the js:

function SubmitForm(frm) {
    frm.submit();
}
Quangahh
  • 61
  • 1
  • 1
  • 1
    This wont work in his case because he had an element named "submit" which effectively replaces the submit function... – Brady Moritz Dec 31 '20 at 20:01
5

form.submit() will not work if the form does not have a <button type="submit">submit</button>. form element belongs to HTMLFormElement interface, therefore, we can call from prototype directly, this method will always work for any form element.

HTMLFormElement.prototype.submit.call(form)
Weilory
  • 2,621
  • 19
  • 35
  • 1
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – CascadiaJS Jan 18 '22 at 22:58
3

This topic has a lot of answers already, but the one that worked best (and simplest - one line!) for me was a modification of the comment made by Neil E. Pearson from Apr 21 2013:

If you're stuck with your submit button being #submit, you can get around it by stealing another form instance's submit() method.

My modification to his method, and what worked for me:

document.createElement('form').submit.call(document.getElementById(frmProduct));

jlemley
  • 533
  • 1
  • 4
  • 15
  • 1
    This is more a workaround than an answer, and ideally you'd just not name things `submit` because you're trampling the kernel. Not that anything in the JS world is ideal :D – Neil May 19 '21 at 23:00
  • I tried this but I get Uncaught TypeError: Illegal invocation – aah134 Mar 16 '22 at 07:46
3

I had same issue and resolved my issue just remove name="submit" from submit button.

<button name='submit' value='Submit Payment' ></button>

Change To

<button value='Submit Payment' ></button>

remove name attribute hope it will work

Siraj Ali
  • 526
  • 6
  • 13
2

giving a form element a name of submit will simple shadow the submit property . make sure you don't have a form element with the name submit and you should be able to access the submit function just fine .

Ahmed Eid
  • 4,414
  • 3
  • 28
  • 34
2

In fact, the solution is very easy...

Original:

    <form action="product.php" method="get" name="frmProduct" id="frmProduct"
enctype="multipart/form-data">
    <input onclick="submitAction()" id="submit_value" type="button" 
    name="submit_value" value="">
</form>
<script type="text/javascript">
    function submitAction()
    {
        document.frmProduct.submit();
    }
</script>

Solution:

    <form action="product.php" method="get" name="frmProduct" id="frmProduct" 
enctype="multipart/form-data">
</form>

<!-- Place the button here -->
<input onclick="submitAction()" id="submit_value" type="button" 
    name="submit_value" value="">

<script type="text/javascript">
    function submitAction()
    {
        document.frmProduct.submit();
    }
</script>
2

Sorry to answer late but for those who are still facing the same error. To get rid of this error:

<form method="POST">
  <input type="text"/>
  <input type="submit" id="submit-form" value="Submit Form" style="display: none;"/>
</form>

<!-- Other element that will submit the form -->
<button onclick="submitTheForm()">Submit the form</button>

<script>
function submitTheForm(){
  document.getElementById("submit-form").click();
}
</script>

Explanation:

The javascript function submitTheForm() is accessing the submit input element and calling the click event on it which results in the form submission.

This solution is lifetime and almost 100% compatible in all browsers.

Stack Overflow
  • 1
  • 5
  • 23
  • 51
1

Possible solutions -
1.Make sure that you don't have any other element with name/id as submit.
2.Try to call the function as onClick = "return submitAction();"
3.document.getElementById("form-name").submit();

Ram Thota
  • 539
  • 5
  • 9
1

You should use this code :

$(document).on("ready", function () {
       
        document.frmProduct.submit();
    });
AnilSengul
  • 33
  • 8
  • While this code snippet may solve the question, [including an explanation](//s.tk/meta/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! – Filnor Dec 06 '18 at 13:25
1

I was facing the same problem that my submit() wasn't working. In my case, I'd an id="submit" on the input tag having type="submit", I removed the id, and it started working.

0

What I used is

var enviar = document.getElementById("enviar");
enviar.type = "submit"; 

Just because everything else didn´t work.

Ryan Bemrose
  • 9,018
  • 1
  • 41
  • 54
0

Solution for me was to set the "form" attribute of button

<form id="form_id_name"><button name="btnSubmit" form="form_id_name" /></form>

or is js:

YOURFORMOBJ.getElementsByTagName("button")[0].setAttribute("form", "form_id_name");
YOURFORMOBJ.submit();
0

I faced this issues also but i made a quick fix using

const form = document.getElementById('create_user_form');

function onSubmit(event) {
  console.log(event.target[0].value);

  console.log(form.submit); // ️ input#submit

  // ✅ Works
  HTMLFormElement.prototype.submit.call(form);
}

form.addEventListener('submit', onSubmit);

Even though accessing the submit property on the form element points to the submit input element and not the method, we can still submit the form by accessing the submit property on the HTMLFormElement interface.

Codertjay
  • 588
  • 8
  • 13
-1

You can try

<form action="product.php" method="get" name="frmProduct" id="frmProduct" enctype="multipart/form-data">

<input onclick="submitAction(this)" id="submit_value" type="button" name="submit_value" value="">

</form>

<script type="text/javascript">
function submitAction(element)
{
    element.form.submit();
}
</script>

Don't you have more than one form with the same name ?

Fabien Ménager
  • 140,109
  • 3
  • 41
  • 60
-1

Use getElementById:

document.getElementById ('frmProduct').submit ()
Ilya Birman
  • 9,834
  • 4
  • 27
  • 31