Categories
JavaScript Answers

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

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *