DEV Community

Cover image for FormData doesn’t include disabled fieldsets
Silvestar Bistrović
Silvestar Bistrović

Posted on • Originally published at silvestar.codes

6

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.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
Enter fullscreen mode Exit fullscreen mode

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)
  })
}
Enter fullscreen mode Exit fullscreen mode

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.

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (5)

Collapse
 
grahamthedev profile image
GrahamTheDev

As intended, plus “disabled” is a terrible experience for screen reader users as the field becomes hidden and you can’t then read the information in it.

Instead use readonly and then you can style it in CSS with [readonly]{} to make it look disabled 👍

Collapse
 
starbist profile image
Silvestar Bistrović

I never thought of it, but this is golden advise. Cheers!

Collapse
 
grahamthedev profile image
GrahamTheDev

No probs! I honestly didn’t know that for data excluded it also so thanks for the TIL! ❤️

Thread Thread
 
starbist profile image
Silvestar Bistrović

Yes, I didn't know that either. That's why I wrote the article and that's why I requested to add it to the MDN article: developer.mozilla.org/en-US/docs/W....

Collapse
 
lexlohr profile image
Alex Lohr

It makes sense, because disabled form fields will not add their data in submission.

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay