Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
JasonC

Form Submission With Javascript

Recommended Posts

Okay, this is driving me crazy. I have a form that I am trying to submit using Javascript. I get a reference to the form with the DOM, add submit() to it, but I get an error in the Firebug console, "TypeError: document.getElementById("loginForm").submit". Any idea why this won't work? Page 379 gives an example where this should work, but also makes the point that "doing this does not actually trigger a submit event." I can get around the problem by generating a click() action on the submit button, but why doesn't submit() work?

Share this post


Link to post
Share on other sites

It doesn't work because the event handler/listener is not called "submit". It's called "onsubmit".

Please change your code as follows, and you should be fine:

 

document.getElementById("loginForm").onsubmit

Share this post


Link to post
Share on other sites

I found the problem. I had "submit" as name and id in the HTML for the submit button, somehow that was messing up the Javascript. Changed the id and name to "submitButton" and now the script works like you would think it would. Good grief!

Share this post


Link to post
Share on other sites

Jason, please note that submit is not an event handler. It's a form method.

 

The submit method is used to submit a form; it's usually used to submit a form without clicking the form submit button. The onsubmit event handler/listener is what fires when the form is submitted.

They are not the same thing, and you should be using onsubmit, not submit.

 

To be honest, I'm not sure how or why your JS code is actually working (but then again, I haven't seen the actual code). Perhaps the browser you're using is able to interpret submit as obsubmit given the context, but either way, that's not what you want.

Please use onsubmit instead.

  • Upvote 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...