Submit a form using JavaScript

I have an html file with a form containing a submit button and an anchor tag that is outside the form. Is it possible to "simulate" a submit button from a binding?

In other words, can a JavaScript function “call” submit for a form on a page?

+3
source share
2 answers

Of course, you can use the method submit()to submit this form:

<a href="#" onclick="document.FormName.submit()">Submit Form</a>

or

<a href="#" onclick="document.getElementById('formID').submit()">Submit Form</a>

To go unobtrusively, you can do this instead:

HTML:

You can specify the id link:

<a href="#" id="link">Submit Form</a>

JavaScript:

<script type="text/javascript">
  var link = document.getElementById('link');

  link.onclick = function(){
    document.getElementById('formID').submit();
    return false;
  };
</script>

where formID- id form.

+16
source

Something like this might work ... (Assuming you give an id to your submit button.)

<a href="#" onclick="document.getElementById('theSubmitButton').click();return false;">Submit form</a>
+3

Source: https://habr.com/ru/post/1781130/


All Articles