SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot dawgbone's Avatar
    Join Date
    Dec 2003
    Location
    Brampton
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Selectbox CSS through Javascript...

    We have a selectbox (aka dropdown menu/optionbox). It is populated through an XML file and I've got that part working.

    There are certain items in the drop down menu that I'd like to implement the style sheet on.

    In html this is rather simple:

    Code:
    <style type="text/css">
    <!--
    BODY {
    	font-size: 11px; }
    .titledrop {
    	font-size: 13px;
    	font-weight: bold;
    	color: #FFFFFF;
    	background-color: #333333;
    	border-top: 1px solid #333333;
    	border-bottom: 1px solid #333333;
    }
    -->
    </style>
    
      <select name="select" id="select">
        <option selected="selected">Choose you Destination</option>
        <option class="titledrop">Category 1</option>
        <option>item 1</option>
        <option>item 2</option>
        <option>item 3</option>
      </select>
    The issue is that because I populate my list through AJAX, I don't have the option to hardcode it.

    Every new option is created by the following line that runs through a loop:

    Code:
    selectDestination.options[selectIndex] = new Option(groupNameValue, groupCode.firstChild.data);
    My question is, how do I apply a style to an individual option? I've already been able to isolate the ones I want to apply the style to (I've added a " - " in front of them).

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can style individual options in several browsers, but IE has some restrictions.

    IE does not allow you to change anything that affects the layout of the displayed text. You can alter the color or the background color, but not the font-size or weight, and you can change the color of a border, but not its style or width.

  3. #3
    SitePoint Zealot dawgbone's Avatar
    Join Date
    Dec 2003
    Location
    Brampton
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am trying to change the background colour of some of the select box options through the javascript.

    The example of what I am trying to do works in all of the browsers I've tested, I'm asking how to do it through javascript.

  4. #4
    SitePoint Zealot dawgbone's Avatar
    Join Date
    Dec 2003
    Location
    Brampton
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyone?

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    Perhaps you could use the proper HTML tag to achieve what you're after.

    Code HTML4Strict:
    <select name="select" id="select">
        <option selected="selected">Choose you Destination</option>
    	<optgroup label="Category 1">
            <option>item 1</option>
            <option>item 2</option>
            <option>item 3</option>
    	</optgroup>
    </select>
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    SitePoint Zealot dawgbone's Avatar
    Join Date
    Dec 2003
    Location
    Brampton
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Again, all the information is populated through an XML file, so I'd need a way to do it through javascript.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •