Can i use javascript to submit a form?

The best way

The best way is to insert an appropriate input tag:


The best JS way

var form = document.getElementById("form-id");

document.getElementById("your-id").addEventListener("click", function () {
  form.submit();
});

Enclose the latter JavaScript code by an DOMContentLoaded event (choose only load for backward compatiblity) if you haven't already done so:

window.addEventListener("DOMContentLoaded", function () {
  var form = document.... // copy the last code block!
});

The easy, not recommandable way (the former answer)

Add an onclick attribute to the link and an id to the form:

submit

All ways

Whatever way you choose, you have call formObject.submit() eventually (where formObject is the DOM object of the

tag).

You also have to bind such an event handler, which calls formObject.submit(), so it gets called when the user clicked a specific link or button. There are two ways:

  • Recommended: Bind an event listener to the DOM object.

    // 1. Acquire a reference to our .
    //    This can also be done by setting :
    //       var form = document.forms.blub;
    
    var form = document.getElementById("form-id");
    
    
    // 2. Get a reference to our preferred element (link/button, see below) and
    //    add an event listener for the "click" event.
    document.getElementById("your-id").addEventListener("click", function () {
      form.submit();
    });
    
  • Not recommended: Insert inline JavaScript. There are several reasons why this technique is not recommendable. One major argument is that you mix markup (HTML) with scripts (JS). The code becomes unorganized and rather unmaintainable.

    submit
    
    
    

Now, we come to the point at which you have to decide for the UI element which triggers the submit() call.

  1. A button

    
    
  2. A link

    submit
    

Apply the aforementioned techniques in order to add an event listener.

Form submit() Method

❮ Form Object

Example

Submit a form:

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

Try it Yourself »


Definition and Usage

The submit() method submits the form (same as clicking the Submit button).

Tip: Use the reset() method to reset the form.


Browser Support

The numbers in the table specify the first browser version that fully supports the method.

Method
submit() Yes Yes Yes Yes Yes

Syntax

formObject.submit()

Parameters

None.

Return Value

No return value.


HTML Tutorial: HTML Forms

JavaScript Tutorial: JS Forms/Validation

❮ Form Object


Summary: in this tutorial, you will learn about JavaScript form API: accessing the form, getting values of the elements, validating form data, and submitting the form.

Form basics

To create a form in HTML, you use the element:

<form action="/signup" method="post" id="signup"> form>

Code language: HTML, XML (xml)

The element has two important attributes: action and method.

  • The action attribute specifies a URL that will process the form submission. In this example, the action is the /signup URL.
  • The method attribute specifies the HTTP method to submit the form with. Usually, the method is either post or get.

Generally, you use the get method when you want to retrieve data from the server and the post method when you want to change data on the server.

JavaScript uses the HTMLFormElement object to represent a form. The HTMLFormElement has the following properties that correspond to the HTML attributes:

  • action – is the URL that processes the form data. It is equivalent to the action attribute of the element.
  • method – is the HTTP method which is equivalent to the method attribute of the element.

The HTMLFormElement element also provides the following useful methods:

  • submit() – submit the form.
  • reset() – reset the form.

Referencing forms

To reference the element, you can use DOM selecting methods such as getElementById():

const form = document.getElementById('subscribe');

Code language: JavaScript (javascript)

An HTML document can have multiple forms. The document.forms property returns a collection of forms (HTMLFormControlsCollection) on the document:

document.forms

Code language: JavaScript (javascript)

To reference a form, you use an index. For example, the following statement returns the first form of the HTML document:

document.forms[0]

Code language: CSS (css)

Submitting a form

Typically, a form has a submit button. When you click it, the browser sends the form data to the server. To create a submit button, you use or

Can I submit a form using JavaScript?

HTML forms can send an HTTP request declaratively. But forms can also prepare an HTTP request to send via JavaScript, for example via XMLHttpRequest .

What is correct way to submit a form using JavaScript?

In the body tag, created an HTML form and specify the id, method, and action of the form. In the form, specify an anchor tag with an event onclick. Create a function for JavaScript that will get executed when the link is clicked. When we click on the link, the function submitForm() will get executed.

What happens when you submit a form in JavaScript?

Essentially, javascript sends the form data (see below examples!) to the specified PHP file, which processes the data and echo 's back a response. You receive that response, break it up into variables, and change the page accordingly.

Can a form action be a JavaScript function?

In an HTML form, the action attribute is used to indicate where the form's data is sent to when it is submitted. The value of this can be set when the form is created, but at times you might want to set it dynamically.