Hiding input fields with type='hidden' vs using a class with display:none

16,629

Solution 1

<input type="hidden"> won't trigger input validation, auto completion, and other user interaction related events. It's designed to save raw data, without user's direct input.

But a <input type="text">, visually hidden, are still going to be considered as a user interaction component. And on some devices enabled visual aid, it will serve as not hidden, and cannot provide the consistency you expected. That's why it's not preferred to do so.

Eg. a <input type="hidden"> won't auto complete it self, or preserve the inputted data before refreshing a page, or prevent the form from being submitted for a failed type validation can't even be seen.

Solution 2

The CSS approach is bad for usability, and accessibility. Think of someone with CSS disabled (very old mobile phones, people with a screen reader), they won't render your CSS as you might expect, and the input with all of its glory would be displayed to the user.

A hidden input should be used for implied user input, meaning, input that would come from the user, but is implied and does not need to be manually entered.

Your question falls more onto the type="hidden" approach.

Solution 3

I'd recommend using <input type="hidden" /> because it is the standard way of HTML to hold a user's input value. If you use another attribute for type and hide it with css, one problem may arise is that when the css fails to load, the input control will show up.

Solution 4

I think your question is about the best approach for debugging form fields. I recommend keeping them as type='hidden' in your HTML. This is best for you, for your users and for semantic interpretation of the page.

Instead, use a tool like the Chrome Developer Tools to do your debugging. This will let you easily see the values of your hidden fields.

Share:
16,629
Rajat Gupta
Author by

Rajat Gupta

A problem solver &amp; enthusiastic programmer looking out for exciting work opportunities. Highly interested in building Android applications, Web development, &amp; Server side coding involving (sql/ nosql) databases &amp; APIs. I love solving problems, building efficient approaches &amp; algorithms. To describe my strong points, I have pretty decent problem solving skills, fast learner &amp; highly motivated &amp; energetic person! Gained good experience in software development in the past 2 years working on numerous android &amp; web development projects with companies like olready.in, twowaits.com, spactre.com. Selected among Top three finalists for LinkedIn MTV GetAJob Season 4 for Flipkart as Software Developer Intern among 50k candidates.

Updated on July 22, 2022

Comments

  • Rajat Gupta
    Rajat Gupta almost 2 years

    There are several hidden form fields on my pages that are used for passing data to server side. For debugging purposes, I feel it easier to hide all eligible input fields by just making them all a part of hidden class than setting the type=hidden attribute on each input field.

    Whenever I need to debug I could easily modify that class attribute to enter debug mode. Ofcourse both the approaches work in hiding the input fields but I am not sure as to why this approach(hiding via class) isn't much widely used in real life. Can you throw some light on what should be preferred approach ?