2

Possible Duplicate:
HTML <select> selected option background-color CSS style

How can I change the color of the selected option in dropdown list? I need to change the color of the option which is visible when the dropdown is closed...

Community
  • 1
  • 1
King Julien
  • 10,981
  • 24
  • 94
  • 132
  • 2
    http://stackoverflow.com/questions/2402146/html-select-selected-option-background-color-css-style – Jawad Jul 11 '11 at 18:16

1 Answers1

3

You can get the value from select.options[select.selectedIndex], so this might fit your needs: http://jsfiddle.net/6VhK8/.

var select = document.getElementById('select');
select.onchange = function() {
    for(var i = 0; i < select.options.length; i++) {
        if(i == select.selectedIndex) {
            select.options[i]
            .style.backgroundColor = 'red';
        } else {
            select.options[i]
            .style.backgroundColor = '';
        }
    }
}
pimvdb
  • 151,816
  • 78
  • 307
  • 352