tags:

views:

271

answers:

1

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  A: 
<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>
Jacob Relkin
That was it! Your change of `type="button"` to `type="submit"` worked. Thanks!
Ross Rogers
i imagine it was the removal of `form.submit()`
seanmonstar
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.
Ross Rogers
@Ross Thanks! :)
Jacob Relkin