Hidden fields don’t have a defaultValue property

I discovered the hard way that hidden HTML form fields don’t have a defaultValue property that you can read. I suppose the presumption is that since the user can’t get at them (without special tools) there was no need.

Here’s what was going on. I recently built an AJAX based admin interface at work. The interface consists of a form, a set of tab links , and a set of divs within the form that individual tab sections would be loaded into, and a hidden field to track what the default mode of the form would be — normally either “insert” or “update” to save the record, but some of the tabs have additional controls to do other things, which work by changing the mode field, and then submitting. Once the action completes, I needed to reset the mode field back to its default value so that a future action would do what it was supposed to.

Except that it wasn’t. The mode field was staying set to whatever state the script had set it to. I’d implemented it by reading the defaultValue of the mode field and setting its value back to that. It had worked great during the earlier part of development, when I had made the mode field a text type input so I could see what was going on. And it worked fine when I changed it back to a text type, but stubbornly refused when it was a hidden type. Even more maddening was that there were no errors… it just didn’t change.

I finally did some searching, and found that the property isn’t available to hidden fields. I ended up using a custom data attribute on the field to track the default value, and read that instead.