JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 23rd, 2013, 11:18 AM
otnj2ee otnj2ee is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Posts: 46 otnj2ee User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 41 m 8 sec
Reputation Power: 8
How to add white space(s) to the option text?

Code:
<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.

Reply With Quote
  #2  
Old February 23rd, 2013, 10:02 PM
web_loone08's Avatar
web_loone08 web_loone08 is online now
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2008
Posts: 605 web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 15 h 33 m 50 sec
Reputation Power: 69
You have to replace the white-space with the escape version of a non-breaking space. Then you have to unescape it, before displaying it in your option tag(s). And that... my friend, will look a little something like this:
Code:
<select id="sel">
<option value=""></option>
</select>

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

Last edited by web_loone08 : February 23rd, 2013 at 10:29 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > How to add white space(s) to the option text?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap