“Submit is not a function” error in JavaScript typically occurs when you are trying to call the submit()
method on an object that is not a form element. The submit()
method is a built-in method of the HTML <form>
element, and it can be used to submit the form’s data to the server.
The error can occur if you have referenced the wrong element in your code, for example, if you have referenced a div
element instead of a form
element. It can also occur if you are trying to call the submit()
method on a JavaScript object that does not have this method.
To fix this error, you need to ensure that you are calling the submit()
method on the correct element. Here’s an example of how to correctly submit a form:
<form id="myForm">
<!-- form inputs -->
</form>
<script>
var form = document.getElementById('myForm');
form.submit();
</script>
You can also use Jquery
$("#formId").submit();
If you are trying to submit the form using JavaScript, you may also check if the form element has a submit method or not, and then call it if possible.
if (typeof form.submit == "function") {
form.submit();
}
In case you are trying to submit the form without refresh the page, you can use the ajax method to submit the form.
$("#formId").on('submit',function(e){
e.preventDefault();
$.ajax({
type: "POST",
url: "submit.php",
data: $("#formId").serialize(),
success: function(response)
{
// Do something after submit
}
});
});
It’s important to investigate the context of the error message and the specific line number and file that it’s being thrown from, it will help you to find the exact cause of the problem and how to fix it.