30

I'm trying to intercept the submission of a form in order to change the value of my keywords label.

I have the following code:

<HTML>
<FORM name="searchForm" method="get" action="tmp.html" >
<input type="text" name="keywords" />
<input type="button" name="submit" value="submit" onclick="formIntercept();"/>
</FORM>
<SCRIPT language="JavaScript">
document.searchForm.keywords.focus();
function formIntercept( ) {
    var f = document.forms['searchForm'];
    f.keywords.value = 'boo';
    f.submit();
};
</SCRIPT>
</HTML>

When I run this in chrome and click the submit button the keywords label changes to boo, but the javascript console says:

 Uncaught TypeError: Property 'submit' of object <#an HtmlFormElement> is not a function.

How can I submit the form with the manipulated keywords?

2
  • 1
    What is an <input type="label" /> or more specifically, what did you really mean to put in the type attribute? Commented Apr 27, 2010 at 4:50
  • oops. you're right. changing type="label" to type="text" Commented Apr 27, 2010 at 4:54

5 Answers 5

115

The reason for the error when trying to call form.submit() is that your submit button is called "submit". This means that the "submit" property of your Form object is now a reference to the submit button, overriding the "submit" method of the form's prototype.

Renaming the submit button would allow you to call the submit() method without error.

Sign up to request clarification or add additional context in comments.

7 Comments

You sir, are a GOD! Never knew about this. I was using $('form :submit').click(); and it was crashing firefox while the Firebug console was open. I tried changing to .submit thinking it would bypass the clicking of the button, but it didn't work at all! I saw this, changed the name of the submit button and HEY PRESTO! It works! No more crashing with firebug open either. XOXOXOXOXOXOX!!!
I think this the answer to the actual question, the error was generated by .submit(), even though the functionality of the application works, the error would've being fixed by changing the name of the submit button without removing f.submit(); Thank you Chris, you rock!
Here is a test tool for checking against this (+ few similar things): kangax.github.com/domlint
Nice validation tool, @HalilÖzgür!
fantastic stuff, I got stuck with this one, and the name of the submit interfering with the submit() method of the form is something I would have never thought of. every days a school day
|
9

The problem is that when some element is <input type="submit" name="submit" /> submit() method will not work. The best solution to this situation is to change the name of that submit type input to something else for example button-submit etc.

Comments

0
<html>
<head></head>
<body>
<form name="searchForm" method="get" action="tmp.html" onsubmit="formIntercept(this);">
<input type="text" name="keywords" />
<input type="submit" name="submit" value="submit"/>
</form>
<script type="text/javascript">
document.searchForm.keywords.focus();
function formIntercept( form ) {
    form.keywords.value = 'boo';
    //form.submit();
}
</script>
</body>
</html>

4 Comments

That was it! Your change of type="button" to type="submit" worked. Thanks!
i imagine it was the removal of form.submit()
Jacob tweaked it further, but merely changing type="button" to type="submit" makes the code work. I'll take his other changes too, however, since it seems less hacky than my way.
I don't think this actually answers the question or even provides the correct work around. The below answer explains why the error occurs.
0

See jQuery .submit().

$("form").submit( function() {
   // TODO tweak your form data before it gets sent
});

Comments

0

Chris Butler explained the issue well.

You can use a native submit method of HTMLFormElement to work around a problem. In your case:

function formIntercept( ) {
    var f = document.forms['searchForm'];
    f.keywords.value = 'boo';
    HTMLFormElement.prototype.submit.call(f);
};

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.