Question

<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)?

Was it helpful?

Solution

You can use:

document.getElementsByTagName('form')[0].submit()

OTHER TIPS

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>

Use this code

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

Docs here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top