References

Beginner-friendly references for web development, with live, editable examples.

The HTML formaction attribute

Attribute All modern browsers Updated
In short

The HTML formaction attribute sets where a form is sent when one particular submit button is clicked. It overrides the form's action for that button only, as in <button formaction="/drafts">Save draft</button>. Buttons without it keep using the form's action.

Overview

The HTML formaction attribute sets the URL a form is sent to when one specific submit button is used. It overrides the form's action attribute for that submission only, so a single form can publish with one button and save a draft with another.

It belongs on submit buttons, meaning a <button> with no type or type="submit", plus <input type="submit"> and <input type="image">. On type="button" or type="reset" it does nothing, because those never submit. The value must be a non-empty URL, and a relative path resolves against the page's base URL.

The Enter key follows its own rule. Pressing Enter in a text field submits through the form's default button, which is the first submit button in the form, so if that button has a formaction, Enter uses its URL too. Put the button you want Enter to trigger first in the markup.

Only the button that submitted the form adds its own name and value to the data. Buttons can therefore share one URL and still be told apart on the server. In JavaScript, the submit event's submitter property points to the same button.

Syntax

<form action="/posts/publish" method="post">
  <label>Title <input type="text" name="title"></label>
  <button>Publish</button>
  <button formaction="/posts/draft">Save draft</button>
</form>

Values

Value
A URL.

Example

Live example
<form id="post" action="/posts/publish" method="post">
  <label for="title">Title</label>
  <input type="text" id="title" name="title" value="Hello">
  <button>Publish</button>
  <button formaction="/posts/draft">Save draft</button>
</form>
<p id="dest">Click a button to see where the form would go.</p>
<script>
document.getElementById('post').addEventListener('submit', function (e) {
  e.preventDefault();
  var url = e.submitter.getAttribute('formaction') || this.getAttribute('action');
  document.getElementById('dest').textContent = 'This form would be sent to ' + url;
});
</script>

Best practices

  • Use formaction for secondary actions such as Save draft or Preview. Keep the main destination on the form's action.
  • Place the button that Enter should trigger first in the source, since implicit submission uses the first submit button.
  • Add formmethod when the second endpoint expects a different HTTP method from the form.
  • Pair a Save draft button with formnovalidate so half-finished forms can still be saved.
  • Check permissions on the server for every endpoint, because anyone can edit a formaction in the browser's developer tools.

Frequently asked questions

What is the formaction attribute used for?
The formaction attribute sends the form to a different URL when that one button submits it. Every other submit button keeps using the form's own action.
Can one form submit to two different URLs?
Yes, one form can post to several URLs through formaction on its buttons. Each submit button with formaction sends the data to its own URL, and the rest use the form's action.
Which URL does the Enter key use when a form has formaction buttons?
Enter submits through the first submit button in the form. If that button has a formaction, the Enter key sends the form to that URL.
Does formaction work on input type=submit?
Yes, formaction works on input type=submit and input type=image as well as on button. It has no effect on type=button or type=reset, since those never submit a form.
How can the server tell which formaction button was clicked?
With formaction buttons, only the clicked button sends its name and value with the form data. Give each button a distinct name or value, and read event.submitter in JavaScript to see the same button.
Does a formaction button still run form validation?
Yes, a formaction button validates the form before sending it, just like a normal submit. Add formnovalidate to the same button when it should skip validation, such as Save draft.