Disabled form inputs do not appear in the request
Include disabled input values in form submissions by simply using hidden fields. Duplicate the disabled input's value into a hidden field having the same name attribute that your backend expects for successful integration.
Understanding browser behavior & workarounds
User agent, Chrome, does not involve disabled inputs in form submissions. To make them part of form submission data, readonly attribute is the rescue tool.
The readonly
fields are non-editable yet retain their ability to grab focus and feature in form submission data.
Tricking form into including data
In scenarios where you must include a certain field's data into submission without allowing it to undergo changes, the readonly attribute works as magic. Flexible fields in dynamic forms can be managed with JavaScript or jQuery just before the form submission hits.
Above code block re-enables the disabled inputs ensuring their submission along with the form data.
Embracing server-power to manage field data
Server-oriented frameworks like ASP.NET MVC with Razor syntax can twist and twirl the form into including all input data, oblivious to whatever state the client-side might be in.
Clone field data without field activation
There can be situations where field activation is just not an option. In these, consider transferring its data into a hidden field just prior to form submission. Here's a JavaScript helper function for the task:
Invoking this function for every disabled input will ensure that no data is left out.
Taking control with AJAX
If form submissions feel restricting, don't fear! AJAX lets you have full authority over form data submission:
With AJAX, you can safely include values from disabled inputs without morphing the form structure using additional hidden fields.
Effective management with client-side scripting
Apply client-side scripting to smartly include disabled input values in form submissions.
Considering tabulation navigation
To maintain the smooth tab navigation, manipulate the readonly attribute in the form submission event with a JavaScript code:
Ensuring data integrity
User has the ability to select and copy values of the read-only fields. To ensure data integrity, impose proper server-side validations.
Addressing accessibility factors
When working with accessibility, it's necessary that elements with readonly
attribute should be distinguishable for all users, including the ones using screen readers.
Deep-diving into HTML standards
For better understanding, consult the HTML specifications:
- HTML 4 spec provides a detailed view of how readonly and disabled behave differently. Readonly fields are included in tabbing navigation and get posted successfully, while disabled do not.
- HTML 5 spec elaborates on building a form data set, shedding light on what really happens under the hood, including what happens for inputs marked as readonly.
Was this article helpful?