#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2005
    Posts
    52
    Rep Power
    10

    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.
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,187
    Rep Power
    184
    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 11:29 PM.

IMN logo majestic logo threadwatch logo seochat tools logo