By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,783 Members | 1,154 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,783 IT Pros & Developers. It's quick & easy.

How to add blank space at the begining of the <option> tag value using javascript

P: 58
Hi Frens,

I want to show a blank space at the begining of the option tag using javascript, can anyone please help me for the same. Let me explain what i want:
I want to have an event which the following code will give

[HTML]<Select>
<option>&nbsp;option1</option>
<option>option2</option>
</select>
[/HTML]
Here the first option would come one postion shifted to right while the second option value will come just touching the left boundary of the option tag.

How can i achieve the same using javascript,

TIA
Feb 5 '08 #1
Share this Question
Share on Google+
5 Replies


acoder
Expert Mod 15k+
P: 16,027
Are you only adding the space or creating the whole select element?
Feb 5 '08 #2

P: 58
Are you only adding the space or creating the whole select element?
Hi acoder,

I am creating a select tag but i am adding the options into it on run time using the javascript. Actually i am adding a kind of indicator at the begining of the option tag's text based on some condition. If the condition fails then i have to leave the place of the indicator blank. I mean to say, suppose i have to display the option tags like this
HTML :
<Select id="mySelect"></select>

JS:
Expand|Select|Wrap|Line Numbers
  1. text1 = '$' + "textOption1";
  2. option1 = new Option(text1 );
  3.   document.getElementById("mySelect").add(option1 );
  4.  
if the above mentioned condition fails for the second option then i am not showing the "$" indicator in the second option but a blank space and the text for it.
Feb 5 '08 #3

acoder
Expert Mod 15k+
P: 16,027
Just declare a variable indicator which, depending on the condition, is either set to "$" or " ". Then set the option text to that.

By the way, your add method is incorrect. It will only work in IE. The standard method uses two arguments. Use a try/catch to support all browsers. See an example.
Feb 5 '08 #4

P: 58
Just declare a variable indicator which, depending on the condition, is either set to "$" or " ". Then set the option text to that.

By the way, your add method is incorrect. It will only work in IE. The standard method uses two arguments. Use a try/catch to support all browsers. See an example.
Hi Acoder,

If i try to set " " at the begining of the option text this is automatically truncated means the blank space is gone....and the text shifts to the left.

Yup i know that the syntax to add the option is wrong i mentioned it here just to explain you the scenario :-)
Feb 6 '08 #5

acoder
Expert Mod 15k+
P: 16,027
In JavaScript, you have to use unicode. So find the unicode character, e.g. " " is \u0020 and use that.
Feb 6 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.