Stop an input field in a form from being submitted
For a quick win, use JavaScript to disable the input on form submission:
This accessibly disables the #excludeInput
from being seen by the server on submission.
For an even lazier option, omit the name
attribute altogether for a hustle-free ride:
This renders the input invisible to form data serialization on submission.
Expanding your options
Becoming interactive smartly
If your input is just for show and should not be modified but viewed, consider making it readonly:
This input field turns into a viewer-only field letting users select and copy data but not modify or submit it.
Working with dynamic forms
Working with forms that accept dynamic inputs? Here's a JavaScript jewel:
This beauty removes the dynamic input from the DOM just in time before submission, keeping your form data intact.
Keeping form data client-side
Need to manage your form data in the client? Exclude fields from the serialized array while submission:
Digging Deeper
Elegantly handling form layout
To make sure your layout doesn't bounce around when you remove or disable fields, creatively use CSS to hold the position:
Ensuring Accessible UX
While cleverly coding those input fields, do consider the accessibility of your form. Make sure your disabled fields interact well with screen readers and the overall user experience is not hampered.
User script maintenance
Using a userscript for modifications in multiple form pages? Keep track of all the changes it incurs for clarity and easy maintenance. Test in multiple environments to ensure uniform functionality. Remember, the spiderweb of coding gets messier when bugs get in!
Was this article helpful?