Categories
JavaScript Answers

How to submit form using an a tag with JavaScript?

Spread the love

To submit form using an a tag with JavaScript, we can call the form’s submit method.

For instance, we write

<form name="myForm" action="handle-data.php">
  Search: <input type="text" name="query" />
  <a href="javascript: submitform()">Search</a>
</form>

to add a form.

Then we write

function submitform() {
  document.myForm.submit();
}

to define the submitForm function which we set as the value of the href attribute of the a element.

In it, we get the by its name with document.myForm.

And then we call submit to submit the form.

Conclusion

To submit form using an a tag with JavaScript, we can call the form’s submit method.

By John Au-Yeung

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

Leave a Reply

Your email address will not be published.