JasonC Posted August 3, 2012 Share Posted August 3, 2012 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? Link to comment Share on other sites More sharing options...
HartleySan Posted August 3, 2012 Share Posted August 3, 2012 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 Link to comment Share on other sites More sharing options...
JasonC Posted August 3, 2012 Author Share Posted August 3, 2012 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! Link to comment Share on other sites More sharing options...
HartleySan Posted August 3, 2012 Share Posted August 3, 2012 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. 1 Link to comment Share on other sites More sharing options...
Recommended Posts