Question

In a drop down list, I need to add spaces in front of the options in the list. I am trying

<select>
<option>&#32;&#32;Sample</option>
</select>

for adding two spaces but it displays no spaces. How can I add spaces before option texts?

Was it helpful?

Solution

Isn't &#160 the entity for space?

<select>
<option>&#160;option 1</option>
<option>    option 2</option>
</select>

Works for me...

EDIT:

Just checked this out, there may be compatibility issues with this in older browsers, but all seems to work fine for me here. Just thought I should let you know as you may want to replace with &nbsp;

OTHER TIPS

I think you want &nbsp; or &#160;

So a fixed version of your example could be...

<select>
  <option>&nbsp;&nbsp;Sample</option>
</select>

or

<select>
  <option>&#160;&#160;Sample</option>
</select>

Use \xA0 with String. This Works Perfect while binding C# Model Data to a Dropdown...

  SectionsList.ForEach(p => { p.Text = "\xA0\xA0Section: " + p.Text; });

As Rob Cooper pointed out, there are some compatibility issues with older browsers (IE6 will display the actual letters "& nbsp;"

This is how I get around it in ASP.Net (I don't have VS open so I'm not sure what characters this actually gets translated to):

Server.HtmlDecode("&nbsp;") 

i tried multiple things but the only thing that worked for me was to use javascript. just notice that i'm using the unicode code for space rather than the html entity, as js doenst know a thing about entities

$("#project_product_parent_id option").each(function(i,option){
  $option = $(option);
  $option.text($option.text().replace(/─/g,'\u00A0\u00A0\u00A0'))
});

You can also press alt+space (on mac) for a non-breaking space. I use it for a Drupal module because Drupal decodes html entities.

I'm nearly certain you can accomplish this with CSS padding, as well. Then you won't be married to the space characters being hard-coded into all of your <option> tags.

@Brian

I'm nearly certain you can accomplish this with CSS padding, as well. Then you won't be married to the space characters being hard-coded into all of your tags.

Good thinking - but unfortunately it doesn't work in (everyone's favourite browser...) IE7 :-(

Here's some code that will work in Firefox (and I assume Op/Saf).

<select>
    <option style="padding-left: 0px;">Blah</option>
        <option style="padding-left: 5px;">Blah</option>
            <option style="padding-left: 10px;">Blah</option>
    <option style="padding-left: 0px;">Blah</option>
        <option style="padding-left: 5px;">Blah</option>
</select>

Just use char 255 (type Alt+2+5+5 on your numeric keypad) with a monospace font like Courier New.

Server.HtmlDecode("&nbsp;") is the only one that worked for me.

Otherwise the chr are printed as text.

I tried to add the padding as a Attribute for the listitem, however it didnt affect it.

&nbsp;

Can you try that? Or is it the same?

I was also having the same issue and I was required to fix this as soon as possible. Though I googled a lot, I was not able to find a quick solution.

Instead I used my own solution, though I am not sure if its appropriate one, it works in my case and exactly which I was required to do.

So when you add an ListItem in dropdown and you want to add space then use the following:-

Press ALT and type 0160 on your numeric keypad, so it should be something like ALT+0160. It will add a space.

ListItem("ALT+0160 ALT+0160 TEST", "TESTVAL")

I tried several of these examples, but the only thing that worked was using javascript, much like dabobert's, but not jQuery, just plain old vanilla javascript and spaces:

for(var x = 0; x < dd.options.length; x++)
{
    item = dd.options[x];
    //if a line that needs indenting
    item.text = '     ' + item.text; //indent
}

This is IE only. IE11 in fact. Ugh.

1.Items Return to List

2.Foreach loop in list

3..

foreach (var item in q)
{
    StringWriter myWriter = new StringWriter();

    myWriter.Lable = HttpUtility.HtmlDecode(item.Label.Replace(" ", "&nbsp;"));
}

This work for me!!!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top