[Solved-5 Solutions] “Submit is not a function” error in JavaScript
Error Description:
When we try to submit a form with JavaScript, we get an error ".submit is not a function" shown.
<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>
click below button to copy the code. By - JavaScript tutorial - team
Also this code:
<script type="text/javascript">
function submitAction()
{
document.forms["frmProduct"].submit();
}
</script>
click below button to copy the code. By - JavaScript tutorial - team
Shows the same error.
Solution 1:
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:
Let the button handle which form object to use:
onclick="return SubmitForm(this.form)"
click below button to copy the code. By - JavaScript tutorial - team
and with the js:
function SubmitForm(frm) {
frm.submit();
}
click below button to copy the code. By - JavaScript tutorial - team
Solution 3:
- 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 4:
If you have no opportunity to change name="submit" you can also submit form this way:
function submitForm(form) {
var submitFormFunction = Object.getPrototypeOf(form).submit;
submitFormFunction.call(form);
}
click below button to copy the code. By - JavaScript tutorial - team
Solution 5:
Use:
var enviar = document.getElementById("enviar");
enviar.type = "submit";