If I have an existing form, I know I can effectively "convert" it to FormData by passing the form to the FormData constructor like in this example:
var myForm = document.getElementById('myForm');
var myFormData = new FormData(myForm);
//Now myFormData contains all fields from myForm
Is there any way to effectively do the inverse of this operation? I have an existing FormData object, and I want to apply its fields to a form in the DOM. Something like myForm.setFormData()
(which I totally made up in) this example:
var myFormData = new FormData();
myFormData.append(...); //etc...
var myForm = document.getElementById('myForm');
myForm.setFormData(myFormData);
//Now myForm contains all fields from myFormData
Is there any way to apply FormData to an existing form?
The comment by Musa is in alignment with the MDN documentation Using FormData Objects:
The
FormData
object lets you compile a set of key/value pairs to send usingXMLHttpRequest
. It is primarily intended for use in sending form data, but can be used independently from forms in order to transmit keyed data.1
Unfortunately, there appears to (currently) be no such method to apply fields from a FormData
object onto a <form>
.
1https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With