4

I want to be able to submit a form after a page loads automatically through Javascript.

I am writing a google chrome extension.

So far the extension can fill in text in the input boxes but I can't figure out a way to get to the next step where the user would click Submit. Please help. Thank you.

EDIT: Pardon my edit. It wasn't clear that he was writing a chrome extension to those of us in the chatroom. --drachenstern

jcolebrand
  • 15,923
  • 10
  • 71
  • 117
milan
  • 2,029
  • 7
  • 24
  • 34

2 Answers2

6

Did you try something like document.getElementById('myForm').submit() ?

With a form like (after EDIT):

<form id="myform" action="/url/To/Action">
  ...
</form>
...
<script>
  document.getElementById('myForm').submit();
</script>

EDIT after your comment:
The script tag must be after the form tag. Or at the very end of the body tag if you want to be sure all the DOM elements are loaded.

Mic
  • 23,536
  • 8
  • 55
  • 69
1

Try document.getElementById('form1').submit();

Sebastien Robert
  • 337
  • 4
  • 11
  • Do you call this line when document is ready ? maybe you can put this line at the bottom of page ? Your form is empty ? – Sebastien Robert Nov 10 '10 at 20:55
  • How can I do it for when the document is ready if I cannot change the HTML? – milan Nov 10 '10 at 20:59
  • Can you use Jquery ? $(document).ready(function(){ // Your code here }); – Sebastien Robert Nov 10 '10 at 21:09
  • hmmm... Sound like a good idea. I wouldn't want to import the jquery library for one function in the end though. Do you know the straight javascript equivalent to $(document).ready(function(){}? – milan Nov 10 '10 at 21:14
  • Sure, you can see example here -> http://stackoverflow.com/questions/799981/document-ready-equivalent-without-jquery – Sebastien Robert Nov 10 '10 at 21:18
  • 1
    To make it work without jQuery, add the `.submit()` command in a `script` tag at the end of the `body` tag. This will ensure that all the DOM is ready before you run your script. – Mic Nov 10 '10 at 21:53