Serhat Ozgel
Serhat Ozgel

Reputation: 23766

How do I put a space character before option text in a HTML select element?

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?

Upvotes: 33

Views: 92794

Answers (15)

Diogo Toscano
Diogo Toscano

Reputation: 11

In PHP you can use html_entity_decode:

obj_dat.options[0] = new Option('Menu 1', 'Menu 1');
obj_dat.options[1] = new Option('<?php echo html_entity_decode('&nbsp;&nbsp;&nbsp;'); ?>Menu 1.1', 'Menu 1.1');

Upvotes: 1

Jay
Jay

Reputation: 19

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!!!

Upvotes: -1

Busch4Al
Busch4Al

Reputation: 53

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.

Upvotes: -1

Rob Cooper
Rob Cooper

Reputation: 28867

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;

Upvotes: 31

Alain Holloway
Alain Holloway

Reputation: 41

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

Upvotes: 4

Shahid Riaz Bhatti
Shahid Riaz Bhatti

Reputation: 421

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")

Upvotes: 2

dabobert
dabobert

Reputation: 929

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'))
});

Upvotes: 8

Riyaz Hameed
Riyaz Hameed

Reputation: 1103

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

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

Upvotes: 31

Gishu
Gishu

Reputation: 136633

&nbsp;

Can you try that? Or is it the same?

Upvotes: 2

user630071
user630071

Reputation: 21

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.

Upvotes: 1

Smolla
Smolla

Reputation: 1751

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.

Upvotes: 5

John
John

Reputation: 17481

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;") 

Upvotes: 9

Ross
Ross

Reputation: 47007

@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>

Upvotes: 4

Brian Warshaw
Brian Warshaw

Reputation: 22984

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.

Upvotes: 4

Matt Sheppard
Matt Sheppard

Reputation: 118013

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>

Upvotes: 15

Related Questions