Search code examples
jsfseam

Change style of certain selectItem in <h:selectOneMenu


In my xhtml page I am using a <h:selectOneMenu> component. In order to populate data, I am using a <s:selectItems> component inside the component. What I need is to conditionally change the style of a certain selectItem. Is it possible or is there an alternative way?
For an example I need to change the color of an option if the label of it is 'pencil'.
I am using jboss-seam and the namespace 's' refers to the seam tag library.

<h:selectOneMenu value="#{bean.selectedItem}" id="myCombo">
   <f:selectItem itemLabel="-Please select-" itemValue=""/>
   <s:selectItems value="#{bean.items}" var="item" label="#{item.name}" itemValue="#{item}"/>
</h:selectOneMenu>

Solution

  • I found an alternative way with javascript. My js code was like this. However this is just a simplified code which shows the concept.

    <script>      
      window.onload = function() {
        var options = document.getElementById("myForm:myCombo").options;
        for(var i = 0; i &lt; options.length; i++) {
          if(options[i].innerHTML == 'pencil') {
            options[i].style.color = 'red';
          }
        }
      }
    </script>