RSS Forums RSS
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 2465 | Replies: 6
Reply
Join Date: Jul 2005
Posts: 177
Reputation: aparnesh is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 10
aparnesh's Avatar
aparnesh aparnesh is offline Offline
Junior Poster

Question Right Align Option Element Text

  #1  
Apr 20th, 2007
I have 3 SELECT elements, one of which contains text and the other two numbers. Values in all 3 selects are generated dynamically from ASP. The Option elements in the selects are being added by a Javascript Code like below

var newOpt ;
newOpt = document.CreateElement("option");
newOpt.text = <%=Value%> ;
document.Form1.Select1.options.add(newOpt) ;

I want the text in the option elements showing Numbers to be right aligned. How can I acheive that ? I cannot use a page wise style for the options as the Select showing text values should be left-aligned.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: Carmel, IN
Posts: 67
Reputation: Sailor_Jerry is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 2
Sailor_Jerry's Avatar
Sailor_Jerry Sailor_Jerry is offline Offline
Junior Poster in Training

Re: Right Align Option Element Text

  #2  
Apr 20th, 2007
after this:
newOpt.text = <%=Value%> ;
you can then check the innerHTML of newOpt var.
Will the <%=Value%> always be in string format?
If not you can use isNaN() which will return true if passed a string.
If it's false then just set the className of the option to your right aligned option class.
You could also use parseInt() if the values will always be passed in as a string.

Then add that option to the DOM
Reply With Quote  
Join Date: Jul 2005
Posts: 177
Reputation: aparnesh is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 10
aparnesh's Avatar
aparnesh aparnesh is offline Offline
Junior Poster

Re: Right Align Option Element Text

  #3  
Apr 23rd, 2007
I tried your solution, but it is not working. Here's the code snippet

Declaring the Class

<STYLE type="text/css">
.GoRight {
	text-align: right ;
	}
</STYLE>

And Here's the code for adding the option and specifying the class name.

newOpt = document.createElement("option");
newOpt.text = <%=mValue%> ;
newOpt.CLASS = 'GoRight';
document.Form2.lstAllowance.options.add(newOpt);

Am I doing something wrong ?
Reply With Quote  
Join Date: Aug 2005
Location: Carmel, IN
Posts: 67
Reputation: Sailor_Jerry is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 2
Sailor_Jerry's Avatar
Sailor_Jerry Sailor_Jerry is offline Offline
Junior Poster in Training

Re: Right Align Option Element Text

  #4  
Apr 23rd, 2007
try newOpt.className = 'GoRight';
Reply With Quote  
Join Date: Jul 2005
Posts: 177
Reputation: aparnesh is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 10
aparnesh's Avatar
aparnesh aparnesh is offline Offline
Junior Poster

Re: Right Align Option Element Text

  #5  
Apr 24th, 2007
Sorry, it's still not working
<STYLE type="text/css">
.GoRight {
	text-align: right ;
	}
</STYLE>

newOpt = document.createElement("option");
newOpt.text = <%=mValue%> ;
newOpt.className = 'GoRight';
document.Form2.lstAllowance.options.add(newOpt);
Reply With Quote  
Join Date: Aug 2005
Location: Carmel, IN
Posts: 67
Reputation: Sailor_Jerry is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 2
Sailor_Jerry's Avatar
Sailor_Jerry Sailor_Jerry is offline Offline
Junior Poster in Training

Re: Right Align Option Element Text

  #6  
Apr 24th, 2007
There are a few things you can try here.

When trouble shooting I like to change the background of the element to lime or something. Then it's easier to see the positioning of the element.

If the text-align:right wont work. try padding-left:.5em or margin-left:.5em.

If this does not work put a break point in there or an alert with the value the newOpt class name, to ensure the className is actually being assigned.
Reply With Quote  
Join Date: Jul 2005
Posts: 177
Reputation: aparnesh is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 10
aparnesh's Avatar
aparnesh aparnesh is offline Offline
Junior Poster

Re: Right Align Option Element Text

  #7  
Apr 26th, 2007
I read in a book that the browser ignores all option element style settings inside a select. Not sure if it can be done at all. Will try some experiments. Will post here if succeed.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 10:13 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC