-2
<form class="myform" action="mail.php">
Your name:<br>
<input type="text" name="myform-name"><br><br>
Your file:<br>
<input type="file" name="myform-file"><br><br>
<button type="submit">Submit</button>
</form>

How do I submit this form using vanilla javascript (not jQuery) directly from the code (without user interaction)?

Jasper
  • 5,090
  • 11
  • 34
  • 41

3 Answers3

5

You can use:

document.getElementsByTagName('form')[0].submit()
Felix
  • 37,892
  • 8
  • 43
  • 55
2

Just add a form name in your code:

<form name="myform" class="myform" action="mail.php">
Your name:<br>
<input type="text" name="myform-name"><br>
<button type="submit">Submit</button>
</form>

submit the from from javascript:

<script type="text/javascript">document.myform.submit();</script>
Sidstar
  • 354
  • 2
  • 6
0

Use this code

document.getElementById("my_form_id").submit();

Docs here

Sergey Pekar
  • 8,555
  • 7
  • 47
  • 54