0

I'm trying to submit a form with JavaScript but I get the following error:

TypeError: 'undefined' is not a function (evaluating 'form.submit()')

This is the relevant code:

var form = document.getElementById("form"),
    submit = document.getElementById("submit");
submit.addEventListener("click", function (e) {
    e.preventDefault();
    form.submit();
}, false);

Edit: this is the relevant html code:

<form method="post" action="www.example.com" id="form">
    <input type="submit" id="submit">
</form>

Thanks for your help!

user2872841
  • 101
  • 1
  • 9

1 Answers1

3

The main problem is that you named your submit button as submit. So, when you say

form.submit();

It tries to execute the button object, which is not a function. So, the fix would be to change the id of the submit button.

Working demo

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
  • I'm doing that in the first line of code. – user2872841 Dec 15 '13 at 11:51
  • @user2872841 Well, I believe it may be because you're using an reserved word, change the `id` to something more unique like, `fillform` – MackieeE Dec 15 '13 at 11:53
  • Thanks MackieeE and thefourtheye! I had tried changing the name of the variable to something more unique like mySubmit and it didn't help, but I didn't think the id would interfere with the javascript code. It now works after changing the id. – user2872841 Dec 15 '13 at 12:03