Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-23-2013, 05:15 PM   PM User | #1
otnj2ee
Regular Coder

 
Join Date: Apr 2007
Posts: 174
Thanks: 17
Thanked 0 Times in 0 Posts
otnj2ee is an unknown quantity at this point
How to add white space to the option text

How to add white space(s) to the option text?

<select id="sel">
<option value=""></option>
</select>

<script>
var text ="abc xyz";
var value ="val";
var contrlObj = document.getElementById("sel");
var newOpt = new Option(text,value);
contrlObj.options[0] = newOpt;

</script>

No matter how many spaces I inserted, it always displayed as: abc xyz.

What I intend to to is to display it as abc xyz with multiple white spaces. I tried abc &nbsp;xyz, and it displayed as abc &nbsp;xyz.

How can I do it?


Thanks to help.
otnj2ee is offline   Reply With Quote
Old 02-23-2013, 05:25 PM   PM User | #2
Airblader
Regular Coder

 
Join Date: Jan 2013
Location: Germany
Posts: 365
Thanks: 3
Thanked 43 Times in 43 Posts
Airblader can only hope to improve
Why would you want to have superfluous whitespaces there anyway? If you just want the select field to be wider, use CSS to style it.
Airblader is online now   Reply With Quote
Old 02-23-2013, 05:57 PM   PM User | #3
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,764
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
I would tend to agree with previous post,
but if you really, really, really want it
see: http://www.webdeveloper.com/forum/sh...t&daysprune=30
jmrker is offline   Reply With Quote
Old 02-23-2013, 06:00 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,042
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
After some experimentation I think that is not possible, at least in IE. Multiple spaces, however expressed, are collapsed into a single space. &nbsp; is, as you have found, rendered literally. Same for \u0020.

Using css can style the width of the select box, but cannot assist with adding intermediate spaces as required here.

Quizmaster: Name a character from any one of these four plays - Otheello, Macbeth, Coriolanus and Hamlet, who has a speaking part.
Contestant: Yorick.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is online now   Reply With Quote
Old 02-23-2013, 06:07 PM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,042
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by jmrker View Post
I would tend to agree with previous post,
but if you really, really, really want it
see: http://www.webdeveloper.com/forum/sh...t&daysprune=30
That did not work for me.

Code:
var x = "ABC   ABC"  // three spaces created with ALT255
alert (x);  /// ABC???ABC
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is online now   Reply With Quote
Old 02-23-2013, 06:27 PM   PM User | #6
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 961
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
Quote:
Originally Posted by otnj2ee View Post
How to add white space(s) to the option text
var text ="abc\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0xyz";
Logic Ali is offline   Reply With Quote
The Following 2 Users Say Thank You to Logic Ali For This Useful Post:
otnj2ee (02-23-2013), Philip M (02-23-2013)
Old 02-23-2013, 06:33 PM   PM User | #7
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,042
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
I find that you can change the existing value of a select list option text to include intermediate spaces (which seems a bit of a hack!):-


Code:
<select id = "myselect">
<option value = 0>Select a title...</option>
<option value = 1>Whatever</option>
</select>

<script type = "text/javascript">

function changeText() {
var otxt = document.getElementById("myselect").options[1].text;
var repText = "                  abc                                  xyz"; 
document.getElementById("myselect").options[1].text = repText;
}

changeText();

</script>
As others have said, I am not sure why you would want to do this. I am sure we would all value enlightenment.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 02-23-2013 at 06:49 PM..
Philip M is online now   Reply With Quote
Old 02-23-2013, 06:38 PM   PM User | #8
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,042
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Logic Ali View Post
var text ="abc\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0xyz";
Yep, that works!
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is online now   Reply With Quote
Users who have thanked Philip M for this post:
otnj2ee (02-23-2013)
Old 02-23-2013, 08:39 PM   PM User | #9
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,764
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Exclamation

Quote:
Originally Posted by Philip M View Post
That did not work for me.

Code:
var x = "ABC   ABC"  // three spaces created with ALT255
alert (x);  /// ABC???ABC
I'm not sure why not.
I tried in FF and got the display as I did within the <option> tag

Were you using the ALT key at the same time as you entered 255 on the keyboard?
Also, release the ALT key between entries.

But it does make more sense to use \xAo and it is easier to understand and replicate
if using the code at a later time.

Last edited by jmrker; 02-23-2013 at 08:44 PM..
jmrker is offline   Reply With Quote
Old 02-24-2013, 12:51 AM   PM User | #10
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 961
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
Quote:
Originally Posted by jmrker View Post
But it does make more sense to use \xAo and it is easier to understand and replicate
if using the code at a later time.
Remember it's A followed by zero not 'o'.
Logic Ali is offline   Reply With Quote
Old 02-24-2013, 01:26 AM   PM User | #11
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,764
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Unhappy

Quote:
Originally Posted by Logic Ali View Post
Remember it's A followed by zero not 'o'.
Famously Fast but Failing Phanlanges
Yes it should be: \xA0
jmrker is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:06 PM.


Advertisement
Log in to turn off these ads.