Sub topic 6
Returning results with form method="dialog"
This chapter assumes you can open a dialog with showModal(), you know how <form> and <button> elements behave, and you can attach event listeners. Here you will see how a dialog reports which button the user pressed and what they typed, without a fetch call or a page reload.
What method="dialog" does
A form normally sends its data to a server with GET or POST. A form with method="dialog" does not. When it is submitted, the browser does three things:
- It skips the network request. No navigation happens.
- It closes the
<dialog>that contains the form. - It stores the
valueof the submit button that was used in the dialog'sreturnValueproperty.
After that, the dialog fires its close event. Your script reads returnValue there.
A complete example
Each submit button carries a value. The script uses it to tell the choices apart.
<button id="open">Edit profile</button>
<p id="status">No changes yet.</p>
<dialog id="dlg" aria-labelledby="dlg-title">
<h2 id="dlg-title">Edit profile</h2>
<form method="dialog">
<label>Name <input name="name" required autofocus></label>
<menu>
<button value="cancel" formnovalidate>Cancel</button>
<button value="save">Save</button>
</menu>
</form>
</dialog>
<script>
const dlg = document.getElementById('dlg');
const form = dlg.querySelector('form');
const status = document.getElementById('status');
document.getElementById('open').addEventListener('click', () => {
dlg.returnValue = '';
dlg.showModal();
});
dlg.addEventListener('close', () => {
if (dlg.returnValue === 'save') {
const data = new FormData(form);
status.textContent = 'Saved name: ' + data.get('name');
} else {
status.textContent = 'Closed with: ' + (dlg.returnValue || 'nothing');
}
});
</script>Three details matter in this code.
- The buttons need a
value. Thevalueof the clicked submit button becomesreturnValue. A button without one gives an empty string. - The form data stays in the form. Nothing is sent anywhere, so read the fields yourself.
new FormData(form)collects every named control, and theclosehandler is a good place to call it. returnValueis reset before opening. The property keeps its last value. If you skip the reset, a dialog dismissed with Escape still reports the previous result.
Validation and the Cancel button
Submitting a method="dialog" form runs normal constraint validation. The required input above blocks the Save button until the field has text. The browser shows its usual error bubble and the dialog stays open.
A Cancel button must not be blocked by that check. Adding formnovalidate to the button skips validation for that submission only. Without it, a user with an empty name field cannot cancel.
Other ways to set returnValue
The submit button is not the only source:
| Action | Effect on returnValue |
|---|---|
| Submit with a button that has `value="save"` | Set to `"save"` |
| `dlg.close('dismissed')` | Set to `"dismissed"` |
| `dlg.close()` with no argument | Left unchanged |
| Escape key | Left unchanged. A `cancel` event fires first, then `close`. |
Because Escape and an argument-less close() leave the old value in place, the reset before showModal() is what lets you distinguish "no answer" from "an earlier answer".
Common errors
- The page reloads or navigates. The form is missing
method="dialog", or it sits outside the<dialog>. The attribute only works when the form is a descendant of the dialog. returnValueis always empty. The submit buttons have novalue, or you used<input type="submit">without one. The label of an input does not count, only itsvalueattribute.- Reading
returnValueright aftershowModal(). The call returns immediately and does not wait for the user. Use thecloseevent, or wrap it in a Promise that resolves from that event. - Reading the form too late. The inputs keep their text after the dialog closes. Call
form.reset()after you read them if you want a clean form next time.
Exercise
A dialog contains a form with method="dialog", a required text input, and two buttons: Save (value="save") and Cancel (value="cancel"). The user clicks Cancel with the text input empty, but the dialog does not close. What is the cause, and what is the smallest fix?
Show answer
The Cancel button submits the form, so constraint validation runs and the empty required input blocks the submission. Add the formnovalidate attribute to the Cancel button so that submission skips validation. The dialog then closes and returnValue becomes "cancel".