Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulate clicking a submit in javascript

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

like image 388
milan Avatar asked Feb 25 '23 20:02

milan


2 Answers

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.

like image 67
Mic Avatar answered Mar 06 '23 23:03

Mic


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

like image 23
Sebastien Robert Avatar answered Mar 07 '23 00:03

Sebastien Robert