Submit form fields inside display:none element
Asked Answered
H

5

115

I have a long form that I've broken up into 6 steps. When the form is loaded, all of the steps are loaded, but only the first step is visible. The rest have CSS of display:none so they are hidden. As a step is completed and validated with Javascript, the current step is set to display:none and the new step is set to display:block. On the final step, the user submits the form. However, as expected, only the fields in display:block element on the page are submitted. All the completed fields in the elements with display:none are ignored.

Is there a way to submit the fields inside the display:none elements?

If not, is there another way to achieve the same effect?

Harold answered 29/11, 2011 at 22:8 Comment(1)
What browser did you see this behaviour in? Browsers seem to submit form elements within display: none containers in all my tests.Abaft
S
226

Set them to visibility:hidden and position:absolute instead. The fields will not be sent to the server with display:none, but will be with visibility:hidden. By also toggling "position" to "absolute" you should get the same visual effect.

Update This does not appear to be an issue anymore in any current browser (as of Nov of 2015). Fields are submitted even if display is set to 'none'. Fields that are 'disabled', however, will continue to not be submitted.

Shayshaya answered 29/11, 2011 at 22:9 Comment(19)
Can I do this with jquery slideUp() and slideDown() ?Harold
I'm not very familiar with jQuery. How is display:none being set right now? If you're doing it you should just be able to replace that code with code that sets the position and visibility. If it's hidden away in a framework you'll have to see if there is a way to override the behavior or find another work around.Shayshaya
no you cant do that with SlideDown, because at the end of the animation html item's 'display' will set to 'none'Tidbit
This does not appear to be an issue in any current browser. Fields are submitted even if display is set to 'none'. Fields that are 'disabled', however, will continue to not be submitted.Shayshaya
IE8 (surprise!) still has issues with posting fields that are set to display:none;. The same problem occurs if the field has another HTML element placed on top of it.Tutto
Just before submitting the form, set display: block to all elements.Uprush
@adam0101, Why do you say that display:none stops the browser from submitting the value? It's disabled that prevent submission not display:noneIngar
@Pacerier, yes disabled prevents values from being submitted, but on some older browsers, display:none would also have this effect. Keep in mind that this answer was posted 4 years ago and is only applicable to developers that need to develop for those older browsers.Shayshaya
@adam0101, Which browsers were you referring to?Ingar
@Pacerier, it's been so long that I don't remember which browsers/versions specifically, but a simple google search shows it was a problem with Mozilla and chromium for sure, although I'm pretty sure there were issues with IE6, 7, and possibly 8 too.Shayshaya
This answer needs updating, not everyone checks all of comments if they are hidden.Allegorical
It's 20th January and using latest versions of chromium and Firefox but still cannot post fields that were hidden earlier on loadJuniejunieta
Chrome 49 here, can confirm that submitting input elements within display:none does NOT work.Siftings
@csvan, it works fine for me with Chrome 49 using this jsfiddle. I tried with the div inside and outside the form element.Shayshaya
Using this jsfiddle, you can see that newer Chrome 62 does not submit display:none contained field.Landsturm
Looks like there is not much agreement on which is the current behaviour. I personally just stumbled upon a case where Chrome (68.0.3440.106) submits a control hidden via display:none. My two cents.Solifluction
7 years later, I'm seeing this exact same behaviour in Chrome v78. I have to remove the display:none from the container element for the elements to post.Chum
The same issue right now (2021), does anyone know if there is a way to force the submit of inputs contained into a hidden (display: none) div? I can't change the display attribute to another like visibility without big changes.Algie
For input fields at least, the easiest way to handle different browser behaviors would be to set input type to "hidden".Oversew
C
7

The HTML4, section 17.13.2, says explicitly that even hidden controls using display:none may be valid for submission.

https://www.w3.org/TR/html401/interact/forms.html

So if the browser ignores display:none then it is not fully HTML-capable. I recommend switching for a real browser.

Closelipped answered 12/11, 2018 at 18:33 Comment(4)
A programmer can’t tell visitors which browser too use and I am having this same issue with the latest Firefox. Trying display:none or visibility:hidden but neither work.Nomanomad
In any event, my comment about visibility:hidden (and maybe even display:none) was nonsense as I realized that in my case it is a bug, or rather an oversight, written into the HTML standard. If a checkbox is unchecked and the form submitted, it’s as though it isn’t even there. It not only does not submit nothing (pardon the double-negative) but it does not submit at all.Nomanomad
So it looks like it was not some quirky non-HTML compliant browser then. Case closed.Closelipped
lifesaver - didnt know they had updated thatAbney
S
2

If you find that your input is not submitted with display: none; and you want your element to not occupy space there is another option which I do not see mentioned anywhere.

It seems to work, but only if your element has no children.

display: contents;

Check the browser support as it is a newish CSS feature.

Substantiate answered 2/5, 2021 at 19:55 Comment(0)
D
2

display:none - means the elements are hidden from view of the user. The html still sees them and the elements will still be submitted in the current version of Chrome, despite being hidden.

Disown answered 26/5, 2022 at 13:54 Comment(0)
G
0

There are bugs in many older browsers where "display:none" removes an item in weird ways...like Webkit browsers pre-2012, where "display:none" on say a submit button, then pressing "return", would not submit the form field data to the server. There are also cases where "display:none" added to input fields of type "hidden" to hide them in old IE browsers fail to send that input data to the server.

The consensus should always be to NOT USE "display:none" to hide form field data or any field that is not normally displayed anyway, but which still needs to be seen by say a screen reader, submit form field data, or be read by search engines. I only use "display:none" when I truly want to remove something temporarily from the page but later enable it again.

Below is an alternative to "display:none" that just hides the item visually from the user and removes it completely from the page flow without hiding its content or data:

.hide {
    position: absolute !important;
    top: -9999px !important;
    left: -9999px !important;
    visibility: hidden !important;
}
Gausman answered 13/1, 2021 at 10:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.