👀 Live Preview
Two submit buttons in one form—each can target a different URL:

The formaction attribute in HTML is a powerful tool that lets developers specify the URL where form data should be sent when a particular submit button is clicked. Applied to <button> or <input type="submit"> elements, it overrides the form’s default action for that one submission.
Per-button URL.
button and input.
Absolute or relative.
One form, many targets.
Set URL dynamically.
Fallback on form.
formactionThe primary purpose of the formaction attribute is to give developers more flexibility in handling form submissions. Instead of always sending data to the URL in the form’s action attribute, you can set a different destination URL for a specific submit button or input.
This is useful when one form collects shared data but different buttons should trigger different server handlers—for example, “Save draft” vs “Publish,” or “Approve” vs “Reject.”
formaction affects only the submit control that was clicked. Other buttons in the same form still use the form’s default action.
Add formaction to a submit button or submit input with a URL value:
<form action="/default-handler" method="post">
<input type="submit" value="Default">
<input type="submit" value="Custom" formaction="/custom-handler">
</form><button type="submit">, <input type="submit">, and <input type="image">.action only for that button’s submission.formmethod and formenctype for full per-button control.The formaction attribute accepts a URL as its value, allowing you to define a specific endpoint for the form submission. The specified URL overrides the form’s default action only for the button or input to which formaction is applied.
formaction="/custom-handler" — Relative URL on the same siteformaction="https://api.example.com/save" — Absolute URL to another originformaction="/drafts/save" — Different path for a “Save draft” button<!-- Relative URL -->
<button type="submit" formaction="/archive">Archive</button>
<!-- Absolute URL -->
<button type="submit" formaction="https://example.com/webhook">Send</button>| Item | Details | Notes |
|---|---|---|
| Attribute on | Submit buttons & image inputs | Not on the <form> itself |
| Value | URL string | Absolute or relative |
| Overrides | Form action | Only for clicked button |
| Related attrs | formmethod, formenctype | Per-button overrides |
| JS property | button.formaction | Read/write URL string |
| Fallback | Form action | When formaction is omitted |
| Element | Supported? | Notes |
|---|---|---|
<input type="submit"> | Yes | Most common use |
<button type="submit"> | Yes | Semantic submit buttons |
<input type="image"> | Yes | Image-based submit control |
<form> | No | Forms use action; buttons use formaction |
<input type="text"> | No | Not a submit control |
action vs formaction| Attribute | On | Behavior |
|---|---|---|
action | <form> | Default submission URL for all submit buttons |
formaction | Submit button / input | Overrides action for that button only |
Two submit buttons with different endpoints, save vs publish pattern, and dynamic formaction with JavaScript.
Two submit buttons in one form—each can target a different URL:
Let’s look at a simple example of how to use the formaction attribute in an HTML form:
<form action="/default-handler" method="post">
<input type="text" name="username" placeholder="Enter your username">
<input type="submit" value="Submit to Default Handler">
<input type="submit" value="Submit to Custom Handler" formaction="/custom-handler">
</form>In this example, there are two submit buttons within the form. The first button submits to the default handler specified in the form’s action attribute. The second button overrides that default and submits to /custom-handler via formaction.
A common real-world pattern: one form, two actions:
<form action="/articles/publish" method="post">
<textarea name="body" required></textarea>
<button type="submit" formaction="/articles/draft">Save Draft</button>
<button type="submit">Publish</button>
</form>“Save Draft” sends the same field data to /articles/draft. “Publish” uses the form’s default action at /articles/publish. Both buttons share one set of inputs.
Similar to other attributes, you can dynamically set the formaction attribute using JavaScript when submission behavior should change at runtime:
<button type="submit" id="dynamicButton">Submit</button>
<script>
document.getElementById("dynamicButton").formaction = "/custom-handler";
</script>In this script, the formaction attribute is set to /custom-handler for a button with the id dynamicButton. This lets you adapt the submission URL based on runtime conditions, such as user role or form validation state.
formaction URL.action without requiring JavaScript.The action attribute sets the fallback URL.
Submit controls can point to a different endpoint.
Browser sends data to that button’s URL.
Flexible submissions without duplicating form fields.
The formaction attribute is supported in all modern browsers. It is part of the HTML5 form submission overrides for submit buttons.
All major browsers honor formaction on submit controls.
Bottom line: Use formaction confidently; always keep a valid default action on the form.
formaction when specific buttons must submit to different endpointsaction on the form as a fallbackformmethod when HTTP method should also differ per buttonformaction on non-submit controlsThe formaction attribute provides a convenient way to customize the form submission URL for specific buttons or input elements within an HTML form. By understanding how to use this attribute effectively, you can enhance the functionality and versatility of your web forms.
Keep a sensible default action on the form, label submit buttons clearly, and validate every endpoint on the server. Combine with formmethod and formenctype when you need full per-button control over submission behavior.
formactionBookmark these before adding multi-action forms.
Per submit button.
PurposeAbsolute or relative.
Valuebutton and input.
Scopebutton.formaction
ScriptAlways set fallback.
Securityaction URL for the submit button that was clicked, sending data to a different endpoint.<button type="submit">, <input type="submit">, and <input type="image">.formaction and was used to submit. Other buttons still use the form’s action./custom-handler and absolute URLs like https://example.com/api are valid.buttonElement.formaction = "/custom-handler" or use setAttribute("formaction", url).Practice overriding submission URLs with formaction in the Try It editor.
3 people found this page helpful