HTML formaction Attribute

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 3 Examples
Forms & Input

What You’ll Learn

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.

01

Override action

Per-button URL.

02

Submit Buttons

button and input.

03

URL Value

Absolute or relative.

04

Multi-Endpoint

One form, many targets.

05

JavaScript

Set URL dynamically.

06

Default action

Fallback on form.

Purpose of formaction

The 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.”

💡
Per-button override only

formaction affects only the submit control that was clicked. Other buttons in the same form still use the form’s default action.

📝 Syntax

Add formaction to a submit button or submit input with a URL value:

formaction.html
<form action="/default-handler" method="post">
  <input type="submit" value="Default">
  <input type="submit" value="Custom" formaction="/custom-handler">
</form>

Syntax Rules

  • Valid on submit buttons: <button type="submit">, <input type="submit">, and <input type="image">.
  • Value is a URL string (absolute or relative to the current document).
  • Overrides the parent form’s action only for that button’s submission.
  • Works together with formmethod and formenctype for full per-button control.

💎 Values

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 site
  • formaction="https://api.example.com/save" — Absolute URL to another origin
  • formaction="/drafts/save" — Different path for a “Save draft” button
formaction-values.html
<!-- Relative URL -->
<button type="submit" formaction="/archive">Archive</button>

<!-- Absolute URL -->
<button type="submit" formaction="https://example.com/webhook">Send</button>

⚡ Quick Reference

ItemDetailsNotes
Attribute onSubmit buttons & image inputsNot on the <form> itself
ValueURL stringAbsolute or relative
OverridesForm actionOnly for clicked button
Related attrsformmethod, formenctypePer-button overrides
JS propertybutton.formactionRead/write URL string
FallbackForm actionWhen formaction is omitted

Applicable Elements

ElementSupported?Notes
<input type="submit">YesMost common use
<button type="submit">YesSemantic submit buttons
<input type="image">YesImage-based submit control
<form>NoForms use action; buttons use formaction
<input type="text">NoNot a submit control

Form action vs formaction

AttributeOnBehavior
action<form>Default submission URL for all submit buttons
formactionSubmit button / inputOverrides action for that button only

Examples Gallery

Two submit buttons with different endpoints, save vs publish pattern, and dynamic formaction with JavaScript.

👀 Live Preview

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

Example — Dual Submit Buttons

Let’s look at a simple example of how to use the formaction attribute in an HTML form:

formaction.html
<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>
Try It Yourself

How It Works

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.

Example — Save Draft vs Publish

A common real-world pattern: one form, two actions:

save-publish.html
<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>
Try It Yourself

How It Works

“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.

Dynamic Values with JavaScript

Similar to other attributes, you can dynamically set the formaction attribute using JavaScript when submission behavior should change at runtime:

dynamic-formaction.html
<button type="submit" id="dynamicButton">Submit</button>

<script>
  document.getElementById("dynamicButton").formaction = "/custom-handler";
</script>
Try It Yourself

How It Works

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.

♿ Accessibility

  • Label buttons clearly — When multiple submit buttons exist, use distinct text like “Save draft” and “Publish” so users know what each does.
  • Do not rely on color alone — Destructive actions (e.g. delete) need explicit labels, not just red styling.
  • Announce consequences — For irreversible actions, consider a confirmation step in addition to a separate formaction URL.
  • Keep a sensible default — The primary action should usually use the form’s default action without requiring JavaScript.

🧠 How formaction Works

1

Form defines default action

The action attribute sets the fallback URL.

Markup
2

Button may override with formaction

Submit controls can point to a different endpoint.

Override
3

User clicks a submit button

Browser sends data to that button’s URL.

Submit
=

One form, multiple destinations

Flexible submissions without duplicating form fields.

Browser Support

The formaction attribute is supported in all modern browsers. It is part of the HTML5 form submission overrides for submit buttons.

HTML5 · Fully supported

Universal per-button action override

All major browsers honor formaction on submit controls.

99% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
formaction attribute 99% supported

Bottom line: Use formaction confidently; always keep a valid default action on the form.

💡 Best Practices

✅ Do

  • Use formaction when specific buttons must submit to different endpoints
  • Always provide a default action on the form as a fallback
  • Give each submit button a clear, distinct label
  • Validate URLs server-side regardless of client-side overrides
  • Pair with formmethod when HTTP method should also differ per button

❌ Don’t

  • Put formaction on non-submit controls
  • Trust client-provided URLs without server authorization checks
  • Rely on JavaScript alone to set submission URLs with no HTML fallback
  • Use confusing button labels when multiple submit options exist
  • Forget CSRF protection on every endpoint that receives form data

Conclusion

The 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.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about formaction

Bookmark these before adding multi-action forms.

5
Core concepts
🔗 02

URL value

Absolute or relative.

Value
📄 03

Submit only

button and input.

Scope
⚙️ 04

Dynamic JS

button.formaction

Script
🔒 05

Default action

Always set fallback.

Security

❓ Frequently Asked Questions

It overrides the form’s action URL for the submit button that was clicked, sending data to a different endpoint.
<button type="submit">, <input type="submit">, and <input type="image">.
Only for the button that has formaction and was used to submit. Other buttons still use the form’s action.
Yes. Both relative paths like /custom-handler and absolute URLs like https://example.com/api are valid.
Assign buttonElement.formaction = "/custom-handler" or use setAttribute("formaction", url).
Yes in all modern browsers. IE 10+ also supports formaction on submit controls.

Build multi-action forms

Practice overriding submission URLs with formaction in the Try It editor.

Try dual submit example →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

3 people found this page helpful