javascript - Change background color of drop down menu on select - Stack Overflow

How to change colour of drop down menu. Like if i have a drop down menu with 5 options, when ever i cli

How to change colour of drop down menu. Like if i have a drop down menu with 5 options, when ever i click an option i want that option to change colour so that i can keep track which options i have already selected. ( 5 here is hypothetical, i have bigger list with IP`s and ports as field so cannot remember all the fields i have checked).

Lets just assume my drop down is

<select>
    <option val="">Please choose</option>
    <option val="1">Option 1</option>
    <option val="2">Option 2</option>
    <option val="3">Option 3</option>
    <option val="4">Option 4</option>
    <option val="5">Option 5</option>
</select>

How to change colour of drop down menu. Like if i have a drop down menu with 5 options, when ever i click an option i want that option to change colour so that i can keep track which options i have already selected. ( 5 here is hypothetical, i have bigger list with IP`s and ports as field so cannot remember all the fields i have checked).

Lets just assume my drop down is

<select>
    <option val="">Please choose</option>
    <option val="1">Option 1</option>
    <option val="2">Option 2</option>
    <option val="3">Option 3</option>
    <option val="4">Option 4</option>
    <option val="5">Option 5</option>
</select>
Share Improve this question asked Oct 10, 2014 at 5:54 mughees ilyasmughees ilyas 5261 gold badge10 silver badges28 bronze badges 2
  • Why dont you use check box or sloshbucket? I dont think color coding is a good way of design for usability. Is it the red wire or the green? – Bhabani Sankar Mishra Commented Oct 10, 2014 at 7:05
  • i have atleast 50 items so sloshbucket will take alot of space pare to drop down and check box will make it cumbersome as to use drop down and then check simple highlighting the options in use seems like a good idea – mughees ilyas Commented Oct 10, 2014 at 8:42
Add a ment  | 

3 Answers 3

Reset to default 2

var select = document.getElementById('select');
select.onchange = function() {
  select.options[select.selectedIndex].style.backgroundColor = 'red';
}

var clean = document.getElementById('clean');
clean.onclick = function() {
    for(var i = 0; i < select.options.length; i++) {
      select.options[i].style.backgroundColor = '';  
    }
}
<select id="select">
    <option val="">Please choose</option>
    <option val="1">Option 1</option>
    <option val="2">Option 2</option>
    <option val="3">Option 3</option>
    <option val="4">Option 4</option>
    <option val="5">Option 5</option>
</select>

<button type="button" id="clean">Clean</div>

Try this

 $('yourdropdownid option:selected').css('background-color', 'red');

Here you go:

DEMO

$('select option').click(function(){
    $(this).css('background','yellow');   
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745346295a4623559.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信