Tuesday, 28 February 2023

Add external form elements to FormData

In HTML5 we can use the form attribute on form control elements to have them be part of a form other then the one they're childs of, as described in this answer.

These elements however seem not to be considered when passing the form element to the FormData constructor, as described on MDN.

Is there a way to have these 'external' controls be included in a way that I then can send the complete form data via a fetch Request?

I am aware, that I could loop through the external elements and append them to the FormData instance like so

let formData = new FormData(form);

for (const external of document.body.querySelectorAll('[form="' + form.name + '"]')) {
    formData.append(external.name, external.value);
}

However I hoped for a more convenient way, especially taking in consideration the extra cases and checks I would have to make it work with radiobuttons etc.



from Add external form elements to FormData

No comments:

Post a Comment