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
17k27 gold badges123 silver badges215 bronze badges
asked May 7, 2009 at 5:41
4
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.
answered May 7, 2009 at 11:46
epascarelloepascarello
203k20 gold badges193 silver badges235 bronze badges
11
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,3293 gold badges10 silver badges37 bronze badges
answered May 31, 2014 at 9:00
gopecagopeca
1,59111 silver badges12 bronze badges
0
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);
}
answered Jan 25, 2017 at 8:18
TerbiyTerbiy
5765 silver badges13 bronze badges
1
<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
answered May 7, 2009 at 5:44
Chad GrantChad Grant
44.1k9 gold badges65 silver badges80 bronze badges
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();
}
answered Jun 18, 2012 at 7:40
QuangahhQuangahh
611 silver badge1 bronze badge
1
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)
answered Jan 18, 2022 at 6:05
WeiloryWeilory
2,52316 silver badges33 bronze badges
1
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));
answered May 25, 2017 at 17:54
jlemleyjlemley
5331 gold badge4 silver badges15 bronze badges
2
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
answered Nov 15, 2020 at 7:13
Siraj AliSiraj Ali
5065 silver badges13 bronze badges
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.
answered Apr 8, 2021 at 4:16
Stack OverflowStack Overflow
1,6921 gold badge15 silver badges32 bronze badges
0
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 .
answered Sep 25, 2016 at 17:28
Ahmed EidAhmed Eid
4,3452 gold badges26 silver badges34 bronze badges
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>
answered Jan 24, 2019 at 1:46
2
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();
answered Dec 21, 2017 at 7:12
Ram ThotaRam Thota
5405 silver badges9 bronze badges
You should use this code :
$(document).on("ready", function () {
document.frmProduct.submit();
});
answered Dec 6, 2018 at 13:06
1
What I used is
var enviar = document.getElementById("enviar");
enviar.type = "submit";
Just because everything else didn´t work.
Ryan Bemrose
8,9481 gold badge40 silver badges54 bronze badges
answered Jul 16, 2016 at 8:54
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();
answered Mar 10, 2017 at 13:19
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.
answered Jul 7, 2022 at 13:50
CodertjayCodertjay
5087 silver badges13 bronze badges
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.
answered Feb 17 at 3:05
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 ?
answered May 7, 2009 at 6:37
Fabien MénagerFabien Ménager
140k3 gold badges41 silver badges60 bronze badges
Use getElementById:
document.getElementById ('frmProduct').submit ()
answered May 7, 2009 at 9:18
Ilya BirmanIlya Birman
9,7844 gold badges27 silver badges31 bronze badges
Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article
Ever tried to submit a form, by using a JavaScript? But when you tried to submit the form by using JavaScript, you might be getting a “Submit is not a function” error in the code. Well, don’t panic as of yet. This article is being dedicated to solving that problem of yours.
So what are we waiting for? Let’s dig in.
Example: You will get “Submit is not a function” error in this.
<!DOCTYPE html>
<
html
>
<
head
>
<
title
>“Submit is not a function”
error in JavaScript</
title
>
</
head
>
<
body
>
<
body
style
=
"text-align:center;"
>
<
h2
style
=
"color:green"
>GeeksForGeeks</
h2
>
<
h2
style
=
"color:purple"
>
“Submit is not a function” error
</
h2
>
<
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
=
"CLICK HERE"
>
</
form
>
<
script
type
=
"text/javascript"
>
function submitAction() {
document.frmProduct.submit();
}
</
script
>
</
body
>
<
html
>
The Error:
(The article continues after the image)
There are 5 different types of solution to this problem.
Solution 1:
Simply rename your button’s name to btnSubmit or any other name. Your code will miraculously work. This is because you have already named the submit button or any other element in the code as submit. When the button is named as submit, it is going to override the submit() function on this code.
btnSubmit
Example:
<!DOCTYPE html>
<
html
>
<
head
>
<
title
>
“Submit is not a function” error in JavaScript
</
title
>
</
head
>
<
body
style
=
"text-align:center;"
>
<
h2
style
=
"color:green"
>GeeksForGeeks</
h2
>
<
h2
style
=
"color:purple"
>
“Submit is not a function” error
</
h2
>
<
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
=
"CLICK HERE"
>
</
form
>
<
script
type
=
"text/javascript"
>
function submitAction() {
document.frmProduct.btnSubmit();
}
</
script
>
</
body
>
<
html
>
Solution 2:
Simply let the button handle and decide which object of the form to be used.
onclick="return SubmitForm(this.form)"
Example:
<!DOCTYPE html>
<
html
>
<
head
>
<
title
>
“Submit is not a function”
error in JavaScript
</
title
>
</
head
>
<
body
>
<
body
style
=
"text-align:center;"
>
<
h2
style
=
"color:green"
>
GeeksForGeeks
</
h2
>
<
h2
style
=
"color:purple"
>
“Submit is not a function” error
</
h2
>
<
form
action
=
"product.php"
method
=
"get"
name
=
"frmProduct"
id
=
"frmProduct"
enctype
=
"multipart/form-data"
>
<
input
onclick
=
"return SubmitForm(this.form)"
id
=
"submit_value"
type
=
"button"
name
=
"submit_value"
value
=
"CLICK HERE"
>
</
form
>
<
script
type
=
"text/javascript"
>
function submitAction() {
document.frmProduct.submit();
}
</
script
>
</
body
>
<
html
>
Solution 3:
If there is no name=”submit” or id=”submit” in the form, make sure to remove or edit it. Also by making sure that there will no other form that is having the same name. This will give an error.
Solution 4:
If there are no changes in the output (still getting an error) by implementing the Solution 3 i.e. having a chance to change name=”submit” or id=”submit”, you could also prevent the error by making changes in the JavaScript.
Example:
<!DOCTYPE html>
<
html
>
<
head
>
<
title
>
“Submit is not a
function” error in JavaScript
</
title
>
</
head
>
<
body
style
=
"text-align:center;"
>
<
h2
style
=
"color:green"
>
GeeksForGeeks
</
h2
>
<
h2
style
=
"color:purple"
>
“Submit is not a function” error
</
h2
>
<
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
=
"CLICK HERE"
>
</
form
>
<
script
type
=
"text/javascript"
>
function submitForm(form) {
var submitFormFunction =
Object.getPrototypeOf(form).submit;
submitFormFunction.call(form);
}
</
script
>
</
body
>
<
html
>
Solution 5:
By making some changes in the JavaScript.
Example:
<!DOCTYPE html>
<
html
>
<
head
>
<
title
>
“Submit is not a function”
error in JavaScript
</
title
>
</
head
>
<
body
style
=
"text-align:center;"
>
<
h2
style
=
"color:green"
>
GeeksForGeeks
</
h2
>
<
h2
style
=
"color:purple"
>
“Submit is not a function” error
</
h2
>
<
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
=
"CLICK HERE"
>
</
form
>
<
script
type
=
"text/javascript"
>
function submitForm(form) {
var enviar =
document.getElementById("enviar");
enviar.type = "submit";
}
</
script
>
</
body
>
<
html
>
Output:
When we lead the code
The error message
After using the solutions:
“Submit is not a function” error
Last Updated :
28 Jun, 2019
Like Article
Save Article
HTML Forms have some really strange JavaScript behavior. We’ll often want to add JavaScript to enhance form controls and actions, but sometimes weird things happen, like the submit
function disappearing.
Breaking down the Message
TypeError: form.submit is not a function
TypeError is a subset of JavaScript Error that is thrown when code attempts to do something that does not exist on the target object.
This message indicates that our code expects to have an object with a submit
function. But if the property exists, it was not a function. In this case, we had a reference to a HTMLFormElement
, which should have a submit
function on its prototype.
This is a blocking error, and script execution will stop when this error occurs.
Understanding the Root Cause
Let’s say you have a simple contact form on your website, like this:
<form action="/contact" method="POST" id="myForm"> <input type="text" name="name" placeholder="Name" /> <input type="submit" name="submit" value="Submit" /> </form>
All is well until we try to attach some JavaScript to this form. If we need to inject some asynchronous action before the submit fires, like an AJAX lookup or send an analytics event, the code might look something like:
var form = document.querySelector("#myForm"); form.addEventListener("submit", function (evt) { //... do something cool. form.submit(); });
Boom. TypeError: form.submit is not a function
Wait, what? How is form.submit
not a function? MDN says its a function, WHAT IS EVEN GOING ON? Then we see, hidden in the text is this really important and interesting line:
If a form control has a name or id of submit it will mask the form’s submit method.
Any named form control can be accessed as a property on the form object. In the example above form.name
would reference the Name HTMLInputElement. But we also gave our submit button a name too, so rather than being the submit()
function we want, it gives us a reference to the HTMLInputElement for submit.
HTMLFormElement
is just trying to be helpful, but this is really weird. What happens if we do other important properties, like class
or id
?
<form class="form-control" id="myForm"> <input name="id" /> <input name="className" /> </form> <script> var form = document.querySelector("#myForm"); form.id; // => <input name="id" /> form.className // => <input name="className" /> </script>
That feels like a bug waiting to happen. Even more weird, if we set the properties, like form.id = "foo";
, that still works, even though we cannot get the value because it has been hidden by the input control.
How to Fix It
This is super weird. How do we avoid these problems? How should we be submitting forms and accessing properties?
1. Access the Prototype and Attributes Directly
Despite the simplicity of using form.submit
or form.id
, we should be more explicit about what we want to do. submit
is a function on the prototype, so we should call it there:
var form = document.querySelector('form.my-form'); HTMLFormElement.prototype.submit.call(form);
Similarly, if we want to access attributes on the form element like id
or action
, we should use getAttribute()
:
var form = document.querySelector('form.my-form'); var id = form.getAttribute('id'); var action = form.getAttribute('action')
2. Monitor Your Environment
You’ll need to make sure that the issue is really fixed and that the problem stops happening. Mistakes happen, APIs change, and users are unpredictable. Just because your application works today, doesn’t mean an error won’t be discovered tomorrow. Tracking when exceptional events happen to your users gives you the context to understand and fix bugs faster. Check out TrackJS Error Monitoring for the fastest and easiest way to get started.
The “TypeError: form.submit is not a function” in JavaScript is a common error when we work with a form element. To see the causes and the solutions, read from the beginning to the end of this article.
When does the “TypeError: form.submit is not a function” in JavaScript occur?
This error appears when you use the ‘submit’ value assigned to the name and id of the button or input in the form element. Unfortunately, this ‘submit’ value matches the submit()
attribute in the form element, so JavaScript cannot call the submit()
function. Look closely at the example below to understand the main cause of the error.
Index.html:
<body>
<main>
<p style="display: none">Form is submitted!</p>
<form id="my-form" action="#" method="post">
<input
class="input-text"
type="text"
name="text"
value="learnshareit.com"
/>
<button name="submit" id="submit" class="btn">Submit</button>
</form>
</main>
<script type="module" src="script.js"></script>
</body>
Script.js:
const form = document.getElementById('my-form');
const button = document.querySelector('.btn');
// Prevent redirect
form.addEventListener('submit', function (e) {
e.preventDefault();
});
button.onclick = function () {
// Error: form.submit is not a function
form.submit();
};
// Show message if successful submit
const query = window.location.search;
if (query.includes('text')) {
document.querySelector('p').style.display = 'block';
form.text.value = '';
}
Output:
TypeError: form.submit is not a function
How to fix this error?
Don’t use ‘submit’ for name and id
Fixing this error is very simple. Just remember not to use the ‘submit’ value for the id or name of any element in the form. In our case, change the name and id to something else.
Index.html:
<body>
<main>
<p style="display: none">Form is submitted!</p>
<form id="my-form" action="" method="GET">
<input
class="input-text"
type="text"
name="text"
value="learnshareit.com"
/>
<!-- changed name and id -->
<button name="submit-btn" id="submit-btn" class="btn">Submit</button>
</form>
</main>
<script type="module" src="script.js"></script>
</body>
Script.js:
const form = document.getElementById('my-form');
const button = document.querySelector('.btn');
// Prevent redirect
form.addEventListener('submit', function (e) {
e.preventDefault();
});
button.onclick = function () {
form.submit();
};
// Show message if successful submit
const query = window.location.search;
if (query.includes('text')) {
document.querySelector('p').style.display = 'block';
form.text.value = '';
}
Output:
Use call()
method
Syntax:
function.call(object, arg1, arg2, ...);
Description:
The call() method allows you to call a function and pass into it an object and arguments. At this point, this keyword in the function will refer to the object.
The cause of the error is that JavaScript calls a function on a button named submit. So we will not call the function normally anymore but will use the call()
method to call the submit()
function in the HTMLFormElement Prototype. Like this:
Index.html:
<body>
<main>
<p style="display: none">Form is submitted!</p>
<form id="my-form" action="" method="GET">
<input
class="input-text"
type="text"
name="text"
value="LearnShareIT"
/>
<!-- Using "submit" value for name and id -->
<button name="submit" id="submit" class="btn">Submit</button>
</form>
</main>
<script type="module" src="script.js"></script>
</body>
Script.js:
const form = document.getElementById('my-form');
const button = document.querySelector('.btn');
// Prevent redirect
form.addEventListener('submit', (e) => {
e.preventDefault();
});
button.onclick = function () {
// Directly call submit() in HTMLFormElement Prototype
HTMLFormElement.prototype.submit.call(form);
};
// Show message if successful submit
const query = window.location.search;
if (query.includes('text')) {
document.querySelector('p').style.display = 'block';
form.text.value = '';
}
Output:
Summary
In this article, we have looked at the leading cause of the “TypeError: form.submit is not a function” in JavaScript. Then, we have shown two simple ways to solve this error. Hopefully, this article will be helpful to you. Thank you for reading!
Maybe you are interested:
- Get the Status Code of a Fetch HTTP Response using JavaScript
- TypeError: getElementById is not a function in JavaScript
- Clear Input fields after Submit using JavaScript
Hi, I’m Cora Lopez. I have a passion for teaching programming languages such as Python, Java, Php, Javascript … I’m creating the free python course online. I hope this helps you in your learning journey.
Name of the university: HCMUE
Major: IT
Programming Languages: HTML/CSS/Javascript, PHP/sql/laravel, Python, Java
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";