Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I submit a form in Javascript with a delay

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>
like image 237
milan Avatar asked Nov 10 '10 19:11

milan


People also ask

How do you dynamically submit a 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” ).

How do you 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.

How do you submit a form is JavaScript without reloading the page?

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.


2 Answers

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.

like image 112
icyrock.com Avatar answered Sep 20 '22 16:09

icyrock.com


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>
like image 34
Rus Mine Avatar answered Sep 18 '22 16:09

Rus Mine