20

Is it possible to wrap long options within a select list?

I have a dynamic select list, and some of the options are pretty lengthy. I'd like options that are too long to wrap to the next line. Beyond that, I'd like to indent those lines.

My solution if this isn't possible is to just trim the result to n characters.

Here's what I have:

I'm a short option
This is a really really really long option
This one isn't too bad
But whoa look how long I am! I go on forever!

And here's what I'd like to have:

I'm a short option
This is a really really 
    really long option
This one isn't too bad
But whoa look how long 
    I am! I go on forever!
KyleMit
  • 30,350
  • 66
  • 462
  • 664
hookedonwinter
  • 12,436
  • 19
  • 61
  • 74
  • Does this answer your question? [Line Break in HTML Select Option?](https://stackoverflow.com/questions/2864238/line-break-in-html-select-option) – Heretic Monkey Nov 29 '21 at 22:53
  • @HereticMonkey Probably, but it was 11 years and 3 jobs ago so I don't remember why I even needed it! Thanks for the link though! – hookedonwinter Nov 30 '21 at 18:26

2 Answers2

14

you cant do this with a standard <option> you will need to roll-your-own or find a menu plugin

Machavity
  • 30,841
  • 27
  • 92
  • 100
Scott Evernden
  • 39,136
  • 15
  • 78
  • 84
2

Here is a quick and pure jQuery solution that may help some. Outside of creating your own drop down and pulling the values/text from a hidden select as mentioned by Scott Evernden. This will give you some room to play with. It doesn't cut off in the middle of a word but at the same time it doesn't wrap the text. It will put the full text into the title so a user may rollover and see the full text word wrapped. It will then use the maxChar setting to go to a certain number of characters then look for the end of the word it is on so as not to cut off the word. The min-width of the option should keep it inline with the select but go ahead and play with the maxChar variable and it should keep it from going outside the bounds. This is a short workaround as I would in most cases go with a customized solution but for quick lists where users can know what they are picking with the first word or two this works great. Hope this helps someone:

var optionText, array = [], newString, maxChar = 40;
$('select').each(function(){
    $(this).find('option').each(function(i,e) {
        $(e).attr('title',$(e).text());
        optionText = $(e).text();
        newString = '';
        if (optionText.length > maxChar) {
            array = optionText.split(' ');
            $.each(array,function(ind,ele) { 
                newString += ele+' ';
                if (ind > 0 && newString.length > maxChar) {
                    $(e).text(newString);
                    return false;
                }
            });

        }
    });
});
Erik Grosskurth
  • 3,762
  • 5
  • 29
  • 44
  • 1
    Worked for me, except for the `title` attribute on the `option` elements, which doesn't appear to be recognized by Chrome anymore. – Mike Sep 05 '16 at 20:45
  • @Mike, what do we do in the case of Chrome? – Steve Mar 11 '18 at 09:24
  • @Steve Besides getting a jquery select plugin that would completely rewrite the ` – Mike Mar 12 '18 at 18:10
  • @Mike - I don't understand why the `title` attribute is mentioned in the code above. Can the code be rewritten to remove `title` ? – Steve Mar 13 '18 at 07:18
  • @Steve Just remove the single line that sets the title. Or just leave it there and for the browsers that support it, it will work as intended. On Chrome the only difference is that for elements that were truncated, you can't get the full text by hovering your mouse over them. This answer is also very old, so it could be that other browsers now act the same as Chrome. I have no idea. – Mike Mar 13 '18 at 21:07
  • Regarding `title` attribute in Chrome, see: https://bugs.chromium.org/p/chromium/issues/detail?id=556048 – bishop Jun 08 '18 at 13:46