1

I'm trying to modify some existing code. I want to use jQuery to change the style of some items in a dropdownlist.

I have my dropdownlist structured like this:

<select name="dept" id="dept">
  <option value="01">DeptA</option>
  <option value="02">DeptB</option>
  <option value="03" class="station">DeptC</option>
  <option value="04" class="station">DeptD</option>
  <option value="05" class="station">DeptE</option>
  <option value="06" class="station">DeptF</option>
  <option value="07" class="station">DeptG</option>
  <option value="08">DeptH</option>
</select>

I want to be able to change the font color or the background color of the option that has class="station" (aka. DeptC-G).

I tried the below method:

<script>
$(function() {    
  $( "#dept" ).selectmenu().selectmenu( "menuWidget" );
  /*$( "option" ).each(function() {
    if ( $( this ).hasClass("station") ) {
      //alert(this.text);
      $(this).addClass("testClass");
    }
  });*/
});
</script>

<style>
  .testClass
  {
    color:blue;
    background:pink;
  }
</style>

The commented section does not work, it will do the alert part, but the style was never applied.

I tried to use

$("#dept").selectmenu().selectmenu( "menuWidget" ).addClass("testClass");

but this will apply the style to every options instead of the specific ones.

I had done some research and I found this which from the question looks very similar to what I'm looking for. However, I tried the given solutions none of them work for me. No errors, just no style was applied.

Any help is appreciated :)

EDIT_01:

This is how it looks like in IE11, the list shift based on the original selection. (I know this is how IE11 render dropdownlist, but we are going to change it)

enter image description here

We will need the dropdownlist to look like what it's like in IE8, like below. And somehow the style was not applied.

enter image description here

1 Answer 1

1

Here is a possible solution:

var options = $('#dept').find('option');

$.each(options, function(i, opt){

if ($(opt).hasClass('station'))
  {
    $(opt).addClass('highlight');
  }
  
});
.highlight
  {
    color:red;
    background:yellow;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="dept" id="dept">
  <option value="01">DeptA</option>
  <option value="02">DeptB</option>
  <option value="03" class="station">DeptC</option>
  <option value="04" class="station">DeptD</option>
  <option value="05" class="station">DeptE</option>
  <option value="06" class="station">DeptF</option>
  <option value="07" class="station">DeptG</option>
  <option value="08">DeptH</option>
</select>

Sign up to request clarification or add additional context in comments.

5 Comments

Similar, but not quite. I will need to run $( "#dept" ).selectmenu().selectmenu( "menuWidget" ); first. This is mainly because we want the dropdownlist to look exactly like IE8 when open in IE11. We were thinking about using jQuery & CSS to draw the dropdownlist that look like IE8. The solution you provide will not work if I run selectmenu()
The .each solution doesn't work either. I added an alert in the if statement, the alert message box did popup, but the style is still not applied. :(
I got your point regarding selectmenu. But, regarding the .each... the code snippet I've provided should work. Run code snippet and see the result.
The style work with your code, but that is not exactly what I want. I will need the dropdownlist to actually dropdown in IE11. If you look at the default rendering in IE11, you will see that the list is will shift up/down depending on the original selection. The way to make the list dropdown, we used $( "#dept" ).selectmenu().selectmenu( "menuWidget" );. If you add list line to your script, you will see what I mean, and you will know that your code doesn't work with this.
Please see my edit, I have include screenshots to explain what I'm looking for. I also posted my code here: collabedit.com/f72ps The top part is the html and the bottom part is my css

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.