TIL - submit() versus requestSubmit() #17
Replies: 1 comment 4 replies
-
Safari doesn't support My workaround is to dispatch a SubmitEvent instead. export type Submitter = HTMLInputElement | HTMLButtonElement;
/**
* Trigger a form submit event with an optional submitter.
* If the submitter is not mounted, it will be appended to the form and removed after submission.
*/
export function requestSubmit(
form: HTMLFormElement | null | undefined,
submitter: Submitter | null = null,
): void {
if (!form) {
throw Error(
"Failed to submit the form. The element provided is null or undefined.",
);
}
if (typeof form.requestSubmit === "function") {
form.requestSubmit(submitter);
} else {
const event = new SubmitEvent("submit", {
bubbles: true,
cancelable: true,
submitter,
});
form.dispatchEvent(event);
}
} |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
TIL - submit() versus requestSubmit()
How the requestSubmit() function differs from submit()
https://www.raymondcamden.com/2024/03/01/til-submit-versus-requestsubmit
Beta Was this translation helpful? Give feedback.
All reactions