I am trying to submit the form automatically with a delay in a chrome extension I am writing and it does not seem to be submitting. Below is my form and javascript:
function submitForm() { // submits form
document.getElementById("ismForm").submit();
}
if (document.getElementById("ismForm")) {
setTimeout("submitForm()", 5000); // set timout
}
<form method="post" id="ismForm" name="ismForm" action="http://www.test.com" class="">
<label for="searchBox">Search </label>
<input type="text" id="searchBox" name="q" value=""> <input type="hidden" id="sayTminLength" value="3"><input type="hidden" id="coDomain" value="US"><input class="button" type="submit" id="search.x" name="search.x" value="Search" autocomplete="off">
</form>
Answer: Use the jQuery submit() Method You can use the submit() method to submit an HTML form (i.e. ) using jQuery. The jQuery code in the following example will submit the form on click of the button (i.e. the element) which has the type attribute set to button (i.e. type=”button” ).
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.
Use jQuery's submit event to handle the form submit, add return false; at the end of the submit handle function to prevent the page to reload.
Don't know the context, but it might be that the page has not been loaded yet completely - you might try putting
if (document.getElementById("ismForm")) {
setTimeout("submitForm()", 5000); // set timout
}
in body onLoad() event. As another thing, try putting as simple alert before setTimeout and at the start of submitForm() to confirm the timeout is getting fired in the first place.
Try this:
<form method="post" action="yourpage/" id="customForm">
<input type="text" name="input1"/>
<input type="text" name="input2"/>
</form>
<button id="submit">SubmitForm</button><!-- Outside of form -->
<script>
function submitForm() {
document.getElementById("customForm").submit()
}
document.getElementById('submit').onclick = function() {
setTimeout(submitForm, 3000);
}
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With