FormData doesn’t include disabled fieldsets

When you disable the fieldset element before reading the form data using the FormData constructor, the data will be empty.

// Find my form
const $form = document.querySelector(‘.my-form’)
// Find all fieldsets inside my form
const $fieldsets = $form…


This content originally appeared on DEV Community and was authored by Silvestar Bistrović

When you disable the fieldset element before reading the form data using the FormData constructor, the data will be empty.

// Find my form
const $form = document.querySelector('.my-form')
// Find all fieldsets inside my form
const $fieldsets = $form.querySelectorAll('fieldset')

// Set all fieldsets as disabled
if($fieldsets.length) {
  $fieldsets.forEach($fieldset => {
    $fieldset.setAttribute('disabled', true)
  })
}

// Construct FormData from the form
const formData = new FormData($form)

// You cannot log formData directly
console.log(Array.from(formData))

// Output: [] - doesn't work

So, if you want to disable the fieldset element, you should do it after using the FormData constructor.

// Find my form
const $form = document.querySelector('.my-form')
// Find all fieldsets inside my form
const $fieldsets = $form.querySelectorAll('fieldset')

// Construct FormData from the form
const formData = new FormData($form)

// You cannot log formData directly
console.log(Array.from(formData))

// Output: [] - works

// Set all fieldsets as disabled
if($fieldsets.length) {
  $fieldsets.forEach($fieldset => {
    $fieldset.setAttribute('disabled', true)
  })
}

Also, if your fields are disabled, they won’t be included in the FormData, too.

Here’s a little demo of what works and what doesn’t work.


This content originally appeared on DEV Community and was authored by Silvestar Bistrović


Print Share Comment Cite Upload Translate Updates
APA

Silvestar Bistrović | Sciencx (2022-01-30T16:19:32+00:00) FormData doesn’t include disabled fieldsets. Retrieved from https://www.scien.cx/2022/01/30/formdata-doesnt-include-disabled-fieldsets/

MLA
" » FormData doesn’t include disabled fieldsets." Silvestar Bistrović | Sciencx - Sunday January 30, 2022, https://www.scien.cx/2022/01/30/formdata-doesnt-include-disabled-fieldsets/
HARVARD
Silvestar Bistrović | Sciencx Sunday January 30, 2022 » FormData doesn’t include disabled fieldsets., viewed ,<https://www.scien.cx/2022/01/30/formdata-doesnt-include-disabled-fieldsets/>
VANCOUVER
Silvestar Bistrović | Sciencx - » FormData doesn’t include disabled fieldsets. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/01/30/formdata-doesnt-include-disabled-fieldsets/
CHICAGO
" » FormData doesn’t include disabled fieldsets." Silvestar Bistrović | Sciencx - Accessed . https://www.scien.cx/2022/01/30/formdata-doesnt-include-disabled-fieldsets/
IEEE
" » FormData doesn’t include disabled fieldsets." Silvestar Bistrović | Sciencx [Online]. Available: https://www.scien.cx/2022/01/30/formdata-doesnt-include-disabled-fieldsets/. [Accessed: ]
rf:citation
» FormData doesn’t include disabled fieldsets | Silvestar Bistrović | Sciencx | https://www.scien.cx/2022/01/30/formdata-doesnt-include-disabled-fieldsets/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.