"Submit is not a function" error in JavaScript

JavascriptHtmlDomSubmit

Javascript Problem Overview


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 :(

Javascript Solutions


Solution 1 - Javascript

> 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.

Solution 2 - Javascript

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);
}

Solution 3 - Javascript

<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

Solution 4 - Javascript

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.

Solution 5 - Javascript

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();
}

Solution 6 - Javascript

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));

Solution 7 - Javascript

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.

Solution 8 - Javascript

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 .

Solution 9 - Javascript

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>

Solution 10 - Javascript

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

Solution 11 - Javascript

You should use this code :

Use that code :

$(document).on("ready", function () {
       
        document.frmProduct.submit();
    });

Solution 12 - Javascript

What I used is

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

Just because everything else didn´t work.

Solution 13 - Javascript

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();

Solution 14 - Javascript

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();

Solution 15 - Javascript

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)

Solution 16 - Javascript

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 ?

Solution 17 - Javascript

Use getElementById:

document.getElementById ('frmProduct').submit ()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJin YongView Question on Stackoverflow
Solution 1 - JavascriptepascarelloView Answer on Stackoverflow
Solution 2 - JavascriptTerbiyView Answer on Stackoverflow
Solution 3 - JavascriptChad GrantView Answer on Stackoverflow
Solution 4 - JavascriptgopecaView Answer on Stackoverflow
Solution 5 - JavascriptQuangahhView Answer on Stackoverflow
Solution 6 - JavascriptjlemleyView Answer on Stackoverflow
Solution 7 - JavascriptStack OverflowView Answer on Stackoverflow
Solution 8 - JavascriptAhmed EidView Answer on Stackoverflow
Solution 9 - JavascriptZurc SoirrabView Answer on Stackoverflow
Solution 10 - JavascriptSiraj AliView Answer on Stackoverflow
Solution 11 - JavascriptAnilSengulView Answer on Stackoverflow
Solution 12 - Javascriptcompraspro.comView Answer on Stackoverflow
Solution 13 - JavascriptProtonIonNeutronView Answer on Stackoverflow
Solution 14 - JavascriptRam ThotaView Answer on Stackoverflow
Solution 15 - JavascriptWeiloryView Answer on Stackoverflow
Solution 16 - JavascriptFabien MénagerView Answer on Stackoverflow
Solution 17 - JavascriptIlya BirmanView Answer on Stackoverflow