Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

formaction — submission URL for a button

The formaction attribute overrides a form's action when a particular button initiates submission. It lets the same form submit to different URLs without changing the form's default destination.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

<button type="submit" formaction="URL">...</button>
<input type="submit" formaction="URL">
<input type="image" src="IMAGE_URL" alt="..." formaction="URL">

formaction specifies the submission URL for a particular submit button: a submitting button, or an input with type="submit" or type="image". It must not be placed on an ordinary button or a reset button.

If the submitting button has formaction, its value overrides the associated form's action for that submission only. Without this attribute, the form's action applies. The value must be a valid, nonempty URL; a relative URL is allowed.

The attribute changes the destination, not the HTTP method, encoding, or validation rules. In the example, the form omits method, so GET is the default. The submit event handler cancels submission and logs only the button label and the fragment of the selected URL: #publish for Publish and #preview for Preview. The example does not send a GET request.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Button-specific form action</title>
</head>
<body>
  <h1>Choose a form action</h1>
  <form id="editor" action="#publish">
    <button type="submit">Publish</button>
    <button type="submit" formaction="#preview">Preview</button>
  </form>
  <p id="publish">Local publish destination.</p>
  <p id="preview">Local preview destination.</p>
  <script>
    const form = document.getElementById('editor');
    form.addEventListener('submit', event => {
      event.preventDefault();
      const button = event.submitter;
      const action = button.hasAttribute('formaction') ? button.formAction : form.action;
      console.log(`${button.textContent}: ${new URL(action).hash}`);
    });
    for (const button of form.querySelectorAll('button')) {
      form.requestSubmit(button);
    }
  </script>
</body>
</html>

Result

Publish: #publish
Preview: #preview

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources