FormDataEvent: formData property
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2021.
TheformData read-only property of theFormDataEventinterface contains theFormData object representing the data contained inthe form when the event was fired.
In this article
Value
AFormData object.
Examples
js
// grab reference to formconst formElem = document.querySelector("form");// submit handlerformElem.addEventListener("submit", (e) => { // on form submission, prevent default e.preventDefault(); // construct a FormData object, which fires the formdata event new FormData(formElem);});// formdata handler to retrieve dataformElem.addEventListener("formdata", (e) => { console.log("formdata fired"); // Get the form data from the event object let data = e.formData; for (const value of data.values()) { console.log(value); } // submit the data via XHR const request = new XMLHttpRequest(); request.open("POST", "/formHandler"); request.send(data);});Specifications
| Specification |
|---|
| HTML> # the-formdataevent-interface:dom-formdataevent-formdata-2> |