user1375096
user1375096

Reputation:

How to display multiple spaces in HTML select list <option></option>

jsfidder here http://jsfiddle.net/a3LwW/4/

My problem is I have multiple spaces in the content of <option>

jQuery val() gives me correct value, but browser renders the <option> as if there is only a single space in the string.

Is there a way to to render multiple spaces correctly in <option>??

I tried <option><pre>three spaces</pre></option>, but it doesn't do what I want.

spaces in select option

HTML

<select id="list">
    <option>one space</option>
    <option>two  spaces</option>
    <option>three   spaces</option>
</select>
<p>Current Value:<pre id="val"></pre></p>

JS

function update_val () {
    $('#val').text($('#list').val()); 
}

$('#list').change(update_val);

//init
update_val();

Upvotes: 7

Views: 15505

Answers (3)

Jan Bar&#225;šek
Jan Bar&#225;šek

Reputation: 31

I think You can use native PHP character like "\xC2\xA0".

More characters you can get by PHP function html_entity_decode:

$nbsp = html_entity_decode('&nbsp;');
$str = 'Hello' . $nbsp . 'World!';

Upvotes: 1

Vinod VT
Vinod VT

Reputation: 7159

Try the whitespace characters other than &nbsp; such as &thinsp;, &ensp;,and &emsp;.

Like

 <option>three &emsp; spaces</option>

Upvotes: 0

SomeKittens
SomeKittens

Reputation: 39532

Use &nbsp; (normally you shouldn't use this for spacing, but you don't have many more options in <select> elements):

<select id="list">
    <option>one space</option>
    <option>two &nbsp;spaces</option>
    <option>three &nbsp;&nbsp;spaces</option>
</select>

Updated Fiddle

Upvotes: 7

Related Questions