How to make a submit out of a link with JavaScript?

Sometimes, we want to make a submit out of a link with JavaScript.

In this article, we’ll look at how to make a submit out of a link with JavaScript.

How to make a submit out of a link with JavaScript?

To make a submit out of a link with JavaScript, we call submit on the form when we click on the link.

For instance, we write

<a href="#" onclick="document.forms['myFormName'].submit(); return false;">
  ...
</a>

to set onclick to the JavaScript code we run to submit the form to submit the form on click.

We use document.forms['myFormName'] to get the form with name or ID myFormName.

And we call submit to submit the form.

Then we return false to stop the default behavior of links.

Conclusion

To make a submit out of a link with JavaScript, we call submit on the form when we click on the link.