0

Here I have the dropdown menu:

 <select name="sourcesSelect" id="{{this.commonID}}" class="custom-select sources" data-color="" placeholder="{{this.status}}">
      <option value="0">In Progress</option>
      <option value="1">Done</option>
      <option value="2">Rejected</option>
    </select>

And this is the css for this select menu by default:

.custom-select-trigger {
    position: relative;
    display: block;
    width: 280px;
    padding: 0 64px 0 20px;
    font-size: 16px;
    font-weight: 300;
    color: #fff;
    line-height: 30px;
    background: #265a88;
    border-radius: 4px;
    cursor: pointer;
  }

What I want: When I choose the option 0, the dropdown color to be changed to green. For option 1 it should change to red and option 2 it should change to yellow.

How can I achieve that?

this is dynanmicaly appliying css on this dropdown menu:

$(".custom-select").each(function() {
var classes = $(this).attr("class"),
    id      = $(this).attr("id"),
    name    = $(this).attr("name");

var template =  '<div class="' + classes + '">';
    template += '<span class="custom-select-trigger">' + $(this).attr("placeholder") + '</span>';
    template += '<div class="custom-options">';
    $(this).find("option").each(function() {
      template += '<span class="custom-option ' + $(this).attr("class") + '" data-value="' + $(this).attr("value") + '">' + $(this).html() + '</span>';
    });
template += '</div></div>';

$(this).wrap('<div class="custom-select-wrapper"></div>');
$(this).hide();
$(this).after(template);
});

$(".custom-option:first-of-type").hover(function() {
$(this).parents(".custom-options").addClass("option-hover");
}, function() {
$(this).parents(".custom-options").removeClass("option-hover");
});

$(".custom-select-trigger").on("click", function() {
$('html').one('click',function() {
  $(".custom-select").removeClass("opened");
});

$(this).parents(".custom-select").toggleClass("opened");
event.stopPropagation();
});

$(".custom-option").on("click", function() {
  $.LoadingOverlay("show");

$(this).parents(".custom-select-wrapper").find("select").val($(this).data("value"));
var id = $(this).parents(".custom-select-wrapper").find("select").attr("id");
var placeholder = $(this).parents(".custom-select-wrapper").find("select").attr("placeholder");
$(this).parents(".custom-options").find(".custom-option").removeClass("selection");
 // alert(placeholder);
$(this).addClass("selection");
$(this).parents(".custom-select").removeClass("opened");
$(this).parents(".custom-select").find(".custom-select-trigger").text($(this).text());
Biku7
  • 450
  • 6
  • 16
  • Does this answer your question? [How to apply background-color to a selected option?](https://stackoverflow.com/questions/18091866/how-to-apply-background-color-to-a-selected-option) – Dshiz Oct 25 '20 at 15:31
  • already tried this. But i dont have the layout and structure of my elements like that so isnt working for me. – Biku7 Oct 25 '20 at 15:34

1 Answers1

0

I think what you can do is give each option a new class/id and or access them as sub-elements in CSS like .custom-select-trigger > option although, I'd prefer the former over the latter as it allows more and individual control over the options. You can write them as

<option value = "0" class = "option_0"> ---Content---</option>

and then you can access this in CSS with the class name. To change the color on interaction, for mouse hover, use

.option_0:hover{background: red;}

or for click

.option_0:active{background: red;}

and do the same for the rest.

  • Its good. .option_0:active{background: red;} But what i want is that to change the color of select menu not the option while i click it. means if i select option 0 then the color of background option menu should be red with text select as : "In Progress" @Purujit – Biku7 Oct 25 '20 at 15:41