0

This is a sample code:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
 xmlns:h="http://java.sun.com/jsf/html"
 xmlns:f="http://java.sun.com/jsf/core"
 xmlns:ui="http://java.sun.com/jsf/facelets"
 xmlns:a4j="http://richfaces.org/a4j"
 xmlns:rich="http://richfaces.org/rich">
<script type="text/javascript" src="js/DataTable.js"></script>
<h:body>
<h:selectOneMenu value="test1"
styleClass="combobox" id="profileID"> 
<f:selectItem itemLabel="--Select--" itemValue="--Select--"/>
<f:selectItem itemLabel="Yes" itemValue="Yes"/>
<f:selectItem itemLabel="No" itemValue="No"/>
</h:selectOneMenu>
</h:body>
</ui:composition>

Now when i select an option, i want the minimized dropdown menu rectangle to change it's background color, highlighting the selected option.If i choose 'Yes', the rectangle should turn 'Green', if i select 'No', the rectangle should turn 'Red'. How can this be implemented?

  • Possible duplicate of [Style – Robert Apr 08 '16 at 07:25

4 Answers4

4

You can keep data-* attribute to store color value for selected option.

document.getElementById('cars').addEventListener('change', function() {
  var bgColor = this.options[this.options.selectedIndex].dataset.color;
  this.style.backgroundColor = bgColor;
  [].forEach.call(this.options, function(option) {
    option.style.backgroundColor = 'white'; //As options will inherit backgroundColor from parent `select`, do set it as `#fff` after every onchange
  });
  this.options[this.options.selectedIndex].style.backgroundColor = bgColor; //If you want selected color of `data-color`
});
<select id="cars">
  <option data-color="green" value="volvo">Volvo</option>
  <option data-color="green" value="saab">Saab</option>
  <option data-color="pink" value="opel">Opel</option>
  <option data-color="blue" value="audi">Audi</option>
</select>
Rayon
  • 36,219
  • 4
  • 49
  • 76
2

This should do it:

$('select').change(function(){
  if (this.value == 'volvo') {
    $(this).css('background','lime');
  }
  if (this.value == 'saab') {
    $(this).css('background','red');
  }
  if (this.value == 'opel') {
    $(this).css('background','blue');
  }
  if (this.value == 'audi') {
    $(this).css('background','yellow');
  }
});
option {
  background: #fff;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
Robert
  • 426
  • 1
  • 4
  • 11
0

Do some jQuery and get the required behaviour.

$("body").on('change', 'select', function(){
            var val = $(this).val();
            if(val == 'volvo'){
                $(this).css("background-color", 'red');
            }
            if(val == 'saab'){
                $(this).css("background-color", 'green');
            }
        })
Rajitha Bandara
  • 743
  • 1
  • 10
  • 16
-1

You can use option:checked to highlight the selected option.

the :checked pseudo-class initially applies to such elements that have the HTML4 selected and checked attributes

Source: w3.org

option:checked {
  background-color: lightgreen;
}
<select>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
</select>
Pugazh
  • 9,453
  • 5
  • 33
  • 54