Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to apply FormData to an existing form?

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?

like image 299
wxactly Avatar asked Oct 21 '22 17:10

wxactly


1 Answers

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 using XMLHttpRequest. 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

like image 115
Sᴀᴍ Onᴇᴌᴀ Avatar answered Oct 27 '22 11:10

Sᴀᴍ Onᴇᴌᴀ