Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to `submit()` an html form after intercepting the submit with javascript

Tags:

javascript

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?

like image 548
Ross Rogers Avatar asked Apr 27 '10 04:04

Ross Rogers


People also ask

Why submit button is not working in HTML?

Sometimes the problem is caused by old versions of the Javascript files, cached by your browser and can be fixed by clearing the browser cache. You can use the browser console of your browser for debugging. After the Javascript error is fixed, the submit button will automatically be enabled.

What is correct way to submit a form using JavaScript?

When we click on the link, the function submitForm() will get executed. This function will get the element object using DOM getElementById() method by passing the form id to this method, then the form will be submitted by using submit() method. Example: Create a form and submit it using the above approach.

Why submit button is not working in PHP?

You need a form surrounding the input. Save this answer. Show activity on this post. You have no form tag and even if you did it would need the method="post" attribute.

How do I enable a form for submit?

Approach: To enable or disable the form submit button, we use the below code snippet. $('#enabled'). click(function () { if ($('#submit-button').is(':disabled')) { $('#submit-button').


2 Answers

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.

like image 182
Chris Butler Avatar answered Sep 17 '22 01:09

Chris Butler


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.

like image 41
Jason Avatar answered Sep 17 '22 01:09

Jason