splitforms.com

HTML · ELEMENT

HTML dialog Element: form method="dialog", showModal() and returnValue

Last reviewed

The <dialog> element is a built-in popup box. Call `dialog.showModal()` to open it as a modal: it sits in the top layer above the page, the rest of the page becomes inert, and Esc closes it. Put a `<form method="dialog">` inside and submitting that form **closes the dialog without any network request**. The `value` of the button that was clicked becomes `dialog.returnValue`, which you read in the `close` event.

  • Live, editable example
  • Copy-paste snippet
  • Attributes & common mistakes
Live demo · nothing is sent

What gets submitted

Fill in the form above and press its button. The demo stops the request and lists every name=value pair the browser would send, plus the controls it leaves out.

With a free access key in the form, these fields land in your splitforms dashboard; access_key and botcheck are read by splitforms, not stored as lead data.

Edit the HTML and re-run it
Runs in a sandbox. Nothing is sent.

Minimal snippet

dialog-example.html
<button id="open">Delete account</button>

<dialog id="confirm" aria-labelledby="confirm-title">
  <form method="dialog">
    <h2 id="confirm-title">Delete your account?</h2>
    <button value="cancel" autofocus>Cancel</button>
    <button value="delete">Delete</button>
  </form>
</dialog>

<script>
  const dialog = document.getElementById("confirm");
  document.getElementById("open").addEventListener("click", () => dialog.showModal());
  dialog.addEventListener("close", () => console.log(dialog.returnValue)); // "cancel" or "delete"
</script>

Copy-paste code

HTML Dialog Element code you can paste today.

Replace YOUR_ACCESS_KEY with a key from your free splitforms dashboard. The form posts to one endpoint — submissions land in your inbox and dashboard with spam filtering built in.

Get free access key
dialog.html
<button type="button" id="open-feedback">Send feedback</button>

<dialog id="feedback" aria-labelledby="feedback-title">
  <!-- method="dialog": submitting closes the dialog, nothing is sent yet -->
  <form method="dialog" id="feedback-form">
    <h2 id="feedback-title">Send us feedback</h2>

    <label for="email">Email</label>
    <input type="email" id="email" name="email" autocomplete="email" required autofocus>

    <label for="message">Message</label>
    <textarea id="message" name="message" rows="4" required></textarea>

    <!-- formnovalidate lets Cancel close the dialog with empty required fields -->
    <button value="cancel" formnovalidate>Cancel</button>
    <button value="send">Send</button>
  </form>
</dialog>

<p id="status" role="status"></p>

<style>
  dialog::backdrop { background: rgb(0 0 0 / 0.5); }
  dialog { border: 0; border-radius: 12px; padding: 1.5rem; max-width: 28rem; }
</style>

<script>
  const ENDPOINT = "https://splitforms.com/api/submit";
  const dialog = document.getElementById("feedback");
  const form = document.getElementById("feedback-form");
  const status = document.getElementById("status");

  document.getElementById("open-feedback").addEventListener("click", () => {
    dialog.returnValue = "";      // reset before each open
    dialog.showModal();           // modal: top layer, page inert, Esc closes
  });

  // Fires after the form submits AND after Esc; focus goes back to the opener
  dialog.addEventListener("close", async () => {
    if (dialog.returnValue !== "send") return;   // "cancel", or "" after Esc

    // method="dialog" sent nothing, so POST the fields ourselves
    const data = new FormData(form);
    data.append("access_key", "YOUR_ACCESS_KEY");

    status.textContent = "Sending...";
    const res = await fetch(ENDPOINT, {
      method: "POST",
      body: data,
      headers: { Accept: "application/json" },
    });
    status.textContent = res.ok ? "Thanks, your feedback was sent." : "Sorry, that did not send. Please try again.";
    if (res.ok) form.reset();
  });
</script>

Comparison

What is the difference between show() and showModal()?

showModal() opens a **modal** dialog: it goes into the top layer above every other element, gets a ::backdrop, makes the rest of the page inert and closes on Esc. show() opens a **non-modal** dialog in normal page flow: the page behind it stays clickable and Esc does nothing. Use showModal() for confirm boxes and forms that need an answer.

What is the difference between show() and showModal()?
TopicshowModal()show()
KindModalNon-modal
PositionTop layer, centred, above any z-indexNormal flow, where the element sits
Rest of the pageInert: no clicks, no focus, hidden from screen readersStill usable
::backdropYesNo
Esc keyFires cancel, then closesDoes nothing
:modal pseudo-classMatchesDoes not match
open attributeSetSet

Sources: WHATWG HTML Standard: The dialog element

form method

Does method="dialog" send data to the server?

No. method="dialog" is the only form method that never makes a request. Submitting the form closes the dialog it sits in and copies the clicked button's value to dialog.returnValue. The fields keep their values, so you can read them with FormData in the close handler and send them with fetch yourself.

Does method="dialog" send data to the server?
MethodNetwork requestWhat happens on submit
getYesNavigates to action?name=value
postYesSends the fields in the request body to action
dialogNoCloses the parent <dialog> and sets returnValue to the submit button's value

Sources: WHATWG HTML Standard: Form submission algorithm

Attributes · <dialog>

Which attributes and properties does the dialog element use?

open and closedby on the dialog, method="dialog" on its form, formmethod="dialog" on one button, and the returnValue property.

HTML Dialog Element attributes and what each one does
AttributeWhat it doesDocs
openPresent while the dialog is shown. Set by show() and showModal(), removed by close(). Don't add it yourself: a dialog shown by setting open is never modal.MDN
closedbyNewer attribute that sets how the user can close it: any (Esc or a click outside), closerequest (Esc only, the default for modals) or none. Check browser support before relying on it.MDN
<form method="dialog">On a form inside the dialog: submitting closes the dialog instead of sending a request, and the clicked button's value becomes returnValue.MDN
<button formmethod="dialog">On one submit button: closes the dialog without a request even when the form uses method="post". Use it for a Cancel button in a form that really posts.MDN
returnValueDOM property, not an attribute. Holds the value of the submit button that closed the dialog, or the string passed to dialog.close(value). It is not cleared on open, so reset it to "" before showModal().MDN

Common mistakes

HTML dialog mistakes to avoid

Expecting method="dialog" to post, setting open by hand, and forgetting that returnValue sticks between opens.

  1. method="dialog" never sends anything

    A form with method="dialog" ignores action and makes no request. If the data must reach a server, read it with new FormData(form) in the close handler and send it with fetch, or give the form method="post" and use formmethod="dialog" only on Cancel.

  2. Setting the open attribute does not make it modal

    <dialog open> or dialog.open = true shows a non-modal dialog with no backdrop, no inert page and no Esc handling. Always call showModal() for a modal.

  3. returnValue is kept from the last close

    Esc closes the dialog without touching returnValue, so it may still say send from the previous time. Set dialog.returnValue = "" before every showModal().

  4. Required fields block Cancel

    A method="dialog" submit still runs validation, so an empty required field stops Cancel from closing the dialog. Add formnovalidate to the Cancel button.

  5. showModal() throws on a dialog that is already open

    Calling showModal() on a dialog opened with show() throws an InvalidStateError. Check if (!dialog.open) first, or close it before reopening.

Sources: WHATWG HTML Standard: The dialog element · MDN: <dialog> · MDN: HTMLDialogElement.returnValue

formmethod="dialog"

How do I add a Cancel button to a dialog form that posts?

Keep method="post" and action on the form so Subscribe really submits, and give only the Cancel button formmethod="dialog". Add formnovalidate so Cancel works while required fields are still empty. Cancel then closes the dialog with returnValue set to cancel and makes no request.

dialog-formmethod.html
<dialog id="subscribe" aria-labelledby="subscribe-title">
  <form action="https://splitforms.com/api/submit" method="POST">
    <input type="hidden" name="access_key" value="YOUR_ACCESS_KEY">
    <h2 id="subscribe-title">Join the newsletter</h2>
    <label for="sub-email">Email</label>
    <input type="email" id="sub-email" name="email" required>

    <button formmethod="dialog" formnovalidate value="cancel">Cancel</button>
    <button type="submit">Subscribe</button>
  </form>
</dialog>

cancel / close

What are the cancel and close events on a dialog?

cancel fires when the user presses Esc on a modal dialog, before it closes; call event.preventDefault() there to keep it open (for example when the form has unsaved input). close fires every time the dialog closes, whether by a method="dialog" submit, dialog.close() or Esc. After Esc, returnValue keeps its old value, which is why you reset it before opening.

Browsers may ignore preventDefault() on a second Esc press in a row, so don't rely on cancel to trap the user.

dialog-events.js
const dialog = document.querySelector("dialog");
const form = dialog.querySelector("form");

dialog.addEventListener("cancel", (event) => {
  const dirty = [...new FormData(form).values()].some((v) => v !== "");
  if (dirty && !confirm("Discard your message?")) event.preventDefault();
});

dialog.addEventListener("close", () => {
  console.log("closed with", dialog.returnValue);
});

::backdrop

How do I style the dialog backdrop?

A modal dialog gets a ::backdrop pseudo-element that covers the page behind it. Give it a semi-transparent background or a blur. To close the dialog on a click outside it, check that the click target is the dialog itself, because clicks on the backdrop are reported on the <dialog> element.

dialog-backdrop.html
<style>
  dialog::backdrop {
    background: rgb(15 23 42 / 0.6);
    backdrop-filter: blur(2px);
  }
</style>

<script>
  const dialog = document.querySelector("dialog");
  // Close on a click outside the box (the backdrop)
  dialog.addEventListener("click", (event) => {
    if (event.target === dialog) dialog.close("cancel");
  });
</script>

Accessibility

How do I make a dialog accessible?

Open it with showModal() so the page behind is inert and focus stays inside. Name it with aria-labelledby pointing at its heading. Put autofocus on the field or button that should get focus first (for a destructive confirm, that is Cancel). When the dialog closes, the browser moves focus back to the element that opened it. <dialog> already has the dialog role, so don't add role="dialog" or aria-modal yourself.

dialog-a11y.html
<button type="button" id="open">Edit name</button>

<dialog id="edit" aria-labelledby="edit-title">
  <form method="dialog">
    <h2 id="edit-title">Edit your name</h2>
    <label for="new-name">Name</label>
    <input id="new-name" name="name" autocomplete="name" autofocus>
    <button value="cancel" formnovalidate>Cancel</button>
    <button value="save">Save</button>
  </form>
</dialog>

Copy-paste ready

The HTML Dialog Element example below is a complete, working form — paste it into any HTML page and it submits, no edits to the markup required.

No backend to build

Your html dialog element form posts to one endpoint. No server, PHP, or API route — accepted submissions are saved to your dashboard before owner notification email is attempted; delivery time and inbox placement depend on provider and recipient settings.

A form that really submits

Unlike a static reference snippet, this <dialog> form actually sends data to a free splitforms backend, with spam filtering built in.

How it works

How to add HTML Dialog Element to any page.

Copy the html dialog element example, drop in your free access key, and paste it into any page — submissions start landing in your dashboard.

Copy the code

Copy the HTML Dialog Element example below.

Add your access key

Replace YOUR_ACCESS_KEY with a free splitforms key from the dashboard.

Paste into your site

Drop the form into any HTML page. Submissions arrive in your dashboard.

How SplitForms works

What happens after someone submits your form?

splitforms receives the submission, filters spam, saves it to your dashboard and emails you — then, on paid plans, sends it on to your webhooks and integrations. No backend infrastructure to build.

A SplitForms contact form submission launching straight to your inbox

Add your endpoint

Point your form to your unique SplitForms endpoint. That's it.

HTML form pointing at a SplitForms submit endpoint

Receive submissions

We instantly capture and organize every submission in your inbox.

Submissions inbox with searchable leads and status pills

Route anywhere

Send data to email, spreadsheets, CRMs, webhooks, and 7,000+ apps.

Generic integration tiles for email, sheets, chat, CRM, automate, and webhook

No credit card required. Set up in under 60 seconds.

Connect & automate

Where can submissions go besides email?

On paid plans, submissions can also go to Slack, Discord, Google Sheets, Notion, Airtable, Zapier or any signed webhook — and splitforms works on the platforms you build on, from WordPress and Shopify to Next.js. With Stripe connected, your forms can take payments too.

Connect your SplitForms form to Slack, Google Sheets, Mailchimp, Zapier and more
NewTake payments on your formsDeposits, bookings, order forms — submitters check out through Stripe and the money goes straight to your Stripe account. Included on every paid plan.See payment forms →Need time slots? Online booking →

Setup guides for the stacks you use

Questions

HTML Dialog Element questions, answered.

View all FAQs
Does method=dialog send data to the server?

No. A form with method="dialog" makes no network request and ignores its action. Submitting it only closes the dialog and sets dialog.returnValue to the clicked button's value. Read the fields with FormData in the close event and send them with fetch if you need them on a server.

What is the difference between show() and showModal()?

showModal() opens a modal dialog in the top layer with a ::backdrop, makes the rest of the page inert and closes on Esc. show() opens a non-modal dialog in normal page flow; the page behind stays usable and Esc does nothing.

How do I get which button closed the dialog?

Give each submit button inside the <form method="dialog"> a value, such as value="cancel" and value="confirm". In the dialog's close event, read dialog.returnValue. After Esc it keeps the previous value, so reset it to an empty string before opening.

How do I close a dialog when the form is submitted?

Use <form method="dialog"> inside the dialog: any submit button closes it. For a form that must post, keep method="post" and put formmethod="dialog" on the Cancel button, or submit with fetch and call dialog.close() when it succeeds.

Do I need role="dialog" or aria-modal on a dialog element?

No. <dialog> has the dialog role built in, and showModal() makes the page behind inert, which is what aria-modal describes. Add aria-labelledby pointing at the dialog's heading so it has a name.

Simple pricing

How much does splitforms cost?

Free gives you 500 submissions for $0 — no card, no time limit. Pro is $5/month for 5,000 submissions a month, and Annual is $49/year for 15,000 a month with every Pro feature.

Free

$0— 500 submissions
 

No card, no time limit

Best for testing

For side projects and indie devs.

  • 500 free submissions
  • Unlimited forms
  • Email notifications included
  • Smart spam filtering
  • Submissions dashboard
  • Redirect after submit
  • MCP server included

Annual

$49/ year
About $4.08 per month, billed yearly
Best value

Every Pro feature with triple the volume — $0.27 per 1,000 at full use.

  • 15,000 submissions per month
  • Unlimited forms
  • Everything in Pro
  • File uploads with 5 GB storage
  • 3 team seats
  • Priority email support

No credit card required on Free • Cancel anytime • Full pricing and FAQ