chosen icon indicating copy to clipboard operation
chosen copied to clipboard

Chosen removes ALL empty options

Open qwelyt opened this issue 9 years ago • 8 comments

If you have several empty options at the top, Chosen removes them all instead of just the first one.

<select>
  <option><option>
  <option><option>
  <option><option>
  <option>Text<option>
</select>

The result when using Chosen on the select is that only one option will be shown, "Text", instead of three, two empty and "Text". It would be nice if Chosen only removed the first element instead of all of them if told to use a placeholder.

To clarify: This happens even if the chosen has not been told to add a placeholder or allow single deselect. It always happens.

qwelyt avatar Jan 28 '15 11:01 qwelyt

Why would you want multiple empty options?

tjschuck avatar Jan 28 '15 16:01 tjschuck

@tjschuck We have a system where the user can add options of their own. due to their requests they want to be able to have empty options. I have no idea why they want that, but they do. And since they are empty, they get put in the top when you do alphabetical sorting. This means they get put in the top fo the select-element, and Chosen removes them.

qwelyt avatar Jan 30 '15 11:01 qwelyt

How do you know which option they select if it has no value whatsoever? I can understand persisting options without text but with a value. But without both there's no use of showing them.

koenpunt avatar Oct 15 '16 13:10 koenpunt

@koenpunt You watch for changes. So if a set value becomes unset (selecting an empty option), you update to empty.

qwelyt avatar Oct 19 '16 09:10 qwelyt

That I understand, but what's the use of multiple empty options if you can't differentiate between them?

koenpunt avatar Oct 19 '16 10:10 koenpunt

@koenpunt Ah. That was just an example. The problem is that Chosen removes all empty options. Not just the first. So if you have two (the actual use case), both are removed. Three empty, all removed. Four, same thing. We don't really have 5 empty once. That part was to highlight that Chosen removes them all.

qwelyt avatar Oct 21 '16 09:10 qwelyt

Chosen removes all empty

True, and maybe that's a bit weird, but in the end it doesn't matter, because in the end an empty option is an empty option.

If you want to support deselecting, you should set the allow_single_deselect option to true.

koenpunt avatar Oct 21 '16 12:10 koenpunt

Bump from the dead. 👋

My own use-case for having an empty starting option (and not wanting to use allow_single_deselect) is to be able to check for a false/null value inside of a PHP application. A select might contain a series of selectable hours, but that hour might be optional, allowing the application to draw some other conclusion.

In this case, having a series of radios does not seem desirable, nor does a specific "Deselect" option make much sense. See below:

Screen Shot 2019-06-05 at 1 14 02 PM

My work-around is for the option's HTML to only contain a non-breaking space, as Chosen does not care if the value attribute is empty - it only cares if the option does not contain any text/html:

<option value="">&nbsp;</option>

This successfully tricks Chosen into rendering the otherwise empty list-item, because Chosen does not escape it's output in this area (which I'll assume is a feature and not a bug.)

This technique even allows for multiple empties if you so desire it (though I do agree with the others that it's probably not a good idea, and will likely lead to some other problems.)

Recommend to close this issue, as the above approach works well within the functionality of Chosen as-is.

Hope this helps someone more than it bothers anyone that is still subscribed. :wave:

JJJ avatar Jun 05 '19 18:06 JJJ