html - Change Style of Specific Option in SelectMenu Using jQuery -
i'm trying modify existing code. want use jquery change style of items in dropdownlist.
i have dropdownlist structured 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 able change font color or background color of option has class="station"
(aka. deptc-g).
i tried below method:
<script> $(function() { $( "#dept" ).selectmenu().selectmenu( "menuwidget" ); /*$( "option" ).each(function() { if ( $( ).hasclass("station") ) { //alert(this.text); $(this).addclass("testclass"); } });*/ }); </script> <style> .testclass { color:blue; background:pink; } </style>
the commented section not work, alert part, style never applied.
i tried use
$("#dept").selectmenu().selectmenu( "menuwidget" ).addclass("testclass");
but apply style every options instead of specific ones.
i had done research , found this question looks similar i'm looking for. however, tried given solutions none of them work me. no errors, no style applied.
any appreciated :)
edit_01:
this how looks in ie11, list shift based on original selection. (i know how ie11 render dropdownlist, going change it)
we need dropdownlist it's in ie8, below. , somehow style not applied.
here 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>
Comments
Post a Comment