How can I set a different color for each option in a select2?












2














Just like the question in the title, but I need to change the background color of select box with each option, do not change anything in dropdown select list.



With "not check-in": background color change to blue, and with "cancel": background color change to pink.



Here is my code:






$(document).ready(function () {
$('.select2').select2({})
});

.wrap-ds-chitiet + .select2-container {
width: 100% !important;
}
.select2-search--dropdown {
display: none !important;
}
.wrap-ds-chitiet + .select2-container .select2-selection {
height: 55px;
border-top-right-radius: 10px;
border-top-left-radius: 10px;
border: none;
font-family: 'Montserrat';
font-weight: 600;
font-size: 0.93rem;
background: linear-gradient(to right, #f8ca6b, #fbad7d);
color: #fff;
}
.wrap-ds-chitiet + .select2-container .select2-selection--single .select2-selection__rendered {
color: #fff;
padding-left: 20px;
padding-top: 13px;
}
.wrap-ds-chitiet + .select2-container--default .select2-selection--single .select2-selection__arrow {
margin-right: 21px;
margin-top: 11px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>

<div>
<select class="form-control select2 wrap-ds-chitiet" id="ds-chitiet-option">
<option>Check-in</option>
<option>Not check-in</option>
<option>Cancel</option>
</select>
</div>












share|improve this question



























    2














    Just like the question in the title, but I need to change the background color of select box with each option, do not change anything in dropdown select list.



    With "not check-in": background color change to blue, and with "cancel": background color change to pink.



    Here is my code:






    $(document).ready(function () {
    $('.select2').select2({})
    });

    .wrap-ds-chitiet + .select2-container {
    width: 100% !important;
    }
    .select2-search--dropdown {
    display: none !important;
    }
    .wrap-ds-chitiet + .select2-container .select2-selection {
    height: 55px;
    border-top-right-radius: 10px;
    border-top-left-radius: 10px;
    border: none;
    font-family: 'Montserrat';
    font-weight: 600;
    font-size: 0.93rem;
    background: linear-gradient(to right, #f8ca6b, #fbad7d);
    color: #fff;
    }
    .wrap-ds-chitiet + .select2-container .select2-selection--single .select2-selection__rendered {
    color: #fff;
    padding-left: 20px;
    padding-top: 13px;
    }
    .wrap-ds-chitiet + .select2-container--default .select2-selection--single .select2-selection__arrow {
    margin-right: 21px;
    margin-top: 11px;
    }

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>

    <div>
    <select class="form-control select2 wrap-ds-chitiet" id="ds-chitiet-option">
    <option>Check-in</option>
    <option>Not check-in</option>
    <option>Cancel</option>
    </select>
    </div>












    share|improve this question

























      2












      2








      2







      Just like the question in the title, but I need to change the background color of select box with each option, do not change anything in dropdown select list.



      With "not check-in": background color change to blue, and with "cancel": background color change to pink.



      Here is my code:






      $(document).ready(function () {
      $('.select2').select2({})
      });

      .wrap-ds-chitiet + .select2-container {
      width: 100% !important;
      }
      .select2-search--dropdown {
      display: none !important;
      }
      .wrap-ds-chitiet + .select2-container .select2-selection {
      height: 55px;
      border-top-right-radius: 10px;
      border-top-left-radius: 10px;
      border: none;
      font-family: 'Montserrat';
      font-weight: 600;
      font-size: 0.93rem;
      background: linear-gradient(to right, #f8ca6b, #fbad7d);
      color: #fff;
      }
      .wrap-ds-chitiet + .select2-container .select2-selection--single .select2-selection__rendered {
      color: #fff;
      padding-left: 20px;
      padding-top: 13px;
      }
      .wrap-ds-chitiet + .select2-container--default .select2-selection--single .select2-selection__arrow {
      margin-right: 21px;
      margin-top: 11px;
      }

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
      <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>

      <div>
      <select class="form-control select2 wrap-ds-chitiet" id="ds-chitiet-option">
      <option>Check-in</option>
      <option>Not check-in</option>
      <option>Cancel</option>
      </select>
      </div>












      share|improve this question













      Just like the question in the title, but I need to change the background color of select box with each option, do not change anything in dropdown select list.



      With "not check-in": background color change to blue, and with "cancel": background color change to pink.



      Here is my code:






      $(document).ready(function () {
      $('.select2').select2({})
      });

      .wrap-ds-chitiet + .select2-container {
      width: 100% !important;
      }
      .select2-search--dropdown {
      display: none !important;
      }
      .wrap-ds-chitiet + .select2-container .select2-selection {
      height: 55px;
      border-top-right-radius: 10px;
      border-top-left-radius: 10px;
      border: none;
      font-family: 'Montserrat';
      font-weight: 600;
      font-size: 0.93rem;
      background: linear-gradient(to right, #f8ca6b, #fbad7d);
      color: #fff;
      }
      .wrap-ds-chitiet + .select2-container .select2-selection--single .select2-selection__rendered {
      color: #fff;
      padding-left: 20px;
      padding-top: 13px;
      }
      .wrap-ds-chitiet + .select2-container--default .select2-selection--single .select2-selection__arrow {
      margin-right: 21px;
      margin-top: 11px;
      }

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
      <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>

      <div>
      <select class="form-control select2 wrap-ds-chitiet" id="ds-chitiet-option">
      <option>Check-in</option>
      <option>Not check-in</option>
      <option>Cancel</option>
      </select>
      </div>








      $(document).ready(function () {
      $('.select2').select2({})
      });

      .wrap-ds-chitiet + .select2-container {
      width: 100% !important;
      }
      .select2-search--dropdown {
      display: none !important;
      }
      .wrap-ds-chitiet + .select2-container .select2-selection {
      height: 55px;
      border-top-right-radius: 10px;
      border-top-left-radius: 10px;
      border: none;
      font-family: 'Montserrat';
      font-weight: 600;
      font-size: 0.93rem;
      background: linear-gradient(to right, #f8ca6b, #fbad7d);
      color: #fff;
      }
      .wrap-ds-chitiet + .select2-container .select2-selection--single .select2-selection__rendered {
      color: #fff;
      padding-left: 20px;
      padding-top: 13px;
      }
      .wrap-ds-chitiet + .select2-container--default .select2-selection--single .select2-selection__arrow {
      margin-right: 21px;
      margin-top: 11px;
      }

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
      <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>

      <div>
      <select class="form-control select2 wrap-ds-chitiet" id="ds-chitiet-option">
      <option>Check-in</option>
      <option>Not check-in</option>
      <option>Cancel</option>
      </select>
      </div>





      $(document).ready(function () {
      $('.select2').select2({})
      });

      .wrap-ds-chitiet + .select2-container {
      width: 100% !important;
      }
      .select2-search--dropdown {
      display: none !important;
      }
      .wrap-ds-chitiet + .select2-container .select2-selection {
      height: 55px;
      border-top-right-radius: 10px;
      border-top-left-radius: 10px;
      border: none;
      font-family: 'Montserrat';
      font-weight: 600;
      font-size: 0.93rem;
      background: linear-gradient(to right, #f8ca6b, #fbad7d);
      color: #fff;
      }
      .wrap-ds-chitiet + .select2-container .select2-selection--single .select2-selection__rendered {
      color: #fff;
      padding-left: 20px;
      padding-top: 13px;
      }
      .wrap-ds-chitiet + .select2-container--default .select2-selection--single .select2-selection__arrow {
      margin-right: 21px;
      margin-top: 11px;
      }

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
      <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>

      <div>
      <select class="form-control select2 wrap-ds-chitiet" id="ds-chitiet-option">
      <option>Check-in</option>
      <option>Not check-in</option>
      <option>Cancel</option>
      </select>
      </div>






      jquery html css twitter-bootstrap-3 jquery-select2






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 at 4:56









      Hana97

      111




      111
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You can use select2:select event to achieve this,



          I have added values to option and created CheckValues common function to add the classes and css classes.



          Please check demo.






          $(document).ready(function() {
          $('.select2').select2()
          $('.select2').on('select2:select', function(e) {
          var data = e.params.data;
          $(".select2-selection").removeClass("defaultColor blueColor blueColor");
          CheckValues(data.element.value)
          console.log(data);
          });

          function CheckValues(value) {
          switch (value) {
          case "checkin":
          $(".select2-selection").addClass("defaultColor");
          break;
          case "notcheckin":
          $(".select2-selection").addClass("blueColor");
          break;
          case "cancel":
          $(".select2-selection").addClass("pinkColor");
          break;
          }
          }

          CheckValues($(".select2").val())

          });

          //With "not check-in": background color change to blue, and with "cancel": background color change to pink.

          .wrap-ds-chitiet+.select2-container {
          width: 100% !important;
          }

          .select2-search--dropdown {
          display: none !important;
          }

          .wrap-ds-chitiet+.select2-container .select2-selection {
          height: 55px;
          border-top-right-radius: 10px;
          border-top-left-radius: 10px;
          border: none;
          font-family: 'Montserrat';
          font-weight: 600;
          font-size: 0.93rem;
          color: #fff;
          }

          .wrap-ds-chitiet+.select2-container .select2-selection--single .select2-selection__rendered {
          color: #fff;
          padding-left: 20px;
          padding-top: 13px;
          }

          .wrap-ds-chitiet+.select2-container--default .select2-selection--single .select2-selection__arrow {
          margin-right: 21px;
          margin-top: 11px;
          }

          .default {
          background: linear-gradient(to right, #f8ca6b, #fbad7d);
          }

          .defaultColor {
          background: linear-gradient(to right, #f8ca6b, #fbad7d);
          }

          .select2-selection--single{
          background-color:transparent;
          }

          .blueColor {
          background: linear-gradient(to right, blue, blue);
          }

          .pinkColor {
          background: linear-gradient(to right, #FFC0CB, #FFC0CB);
          }

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
          <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>

          <div>
          <select class="form-control select2 wrap-ds-chitiet" id="ds-chitiet-option">
          <option value="checkin">Check-in</option>
          <option value="notcheckin">Not check-in</option>
          <option value="cancel">Cancel</option>
          </select>
          </div>








          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405493%2fhow-can-i-set-a-different-color-for-each-option-in-a-select2%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            You can use select2:select event to achieve this,



            I have added values to option and created CheckValues common function to add the classes and css classes.



            Please check demo.






            $(document).ready(function() {
            $('.select2').select2()
            $('.select2').on('select2:select', function(e) {
            var data = e.params.data;
            $(".select2-selection").removeClass("defaultColor blueColor blueColor");
            CheckValues(data.element.value)
            console.log(data);
            });

            function CheckValues(value) {
            switch (value) {
            case "checkin":
            $(".select2-selection").addClass("defaultColor");
            break;
            case "notcheckin":
            $(".select2-selection").addClass("blueColor");
            break;
            case "cancel":
            $(".select2-selection").addClass("pinkColor");
            break;
            }
            }

            CheckValues($(".select2").val())

            });

            //With "not check-in": background color change to blue, and with "cancel": background color change to pink.

            .wrap-ds-chitiet+.select2-container {
            width: 100% !important;
            }

            .select2-search--dropdown {
            display: none !important;
            }

            .wrap-ds-chitiet+.select2-container .select2-selection {
            height: 55px;
            border-top-right-radius: 10px;
            border-top-left-radius: 10px;
            border: none;
            font-family: 'Montserrat';
            font-weight: 600;
            font-size: 0.93rem;
            color: #fff;
            }

            .wrap-ds-chitiet+.select2-container .select2-selection--single .select2-selection__rendered {
            color: #fff;
            padding-left: 20px;
            padding-top: 13px;
            }

            .wrap-ds-chitiet+.select2-container--default .select2-selection--single .select2-selection__arrow {
            margin-right: 21px;
            margin-top: 11px;
            }

            .default {
            background: linear-gradient(to right, #f8ca6b, #fbad7d);
            }

            .defaultColor {
            background: linear-gradient(to right, #f8ca6b, #fbad7d);
            }

            .select2-selection--single{
            background-color:transparent;
            }

            .blueColor {
            background: linear-gradient(to right, blue, blue);
            }

            .pinkColor {
            background: linear-gradient(to right, #FFC0CB, #FFC0CB);
            }

            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
            <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
            <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>

            <div>
            <select class="form-control select2 wrap-ds-chitiet" id="ds-chitiet-option">
            <option value="checkin">Check-in</option>
            <option value="notcheckin">Not check-in</option>
            <option value="cancel">Cancel</option>
            </select>
            </div>








            share|improve this answer




























              0














              You can use select2:select event to achieve this,



              I have added values to option and created CheckValues common function to add the classes and css classes.



              Please check demo.






              $(document).ready(function() {
              $('.select2').select2()
              $('.select2').on('select2:select', function(e) {
              var data = e.params.data;
              $(".select2-selection").removeClass("defaultColor blueColor blueColor");
              CheckValues(data.element.value)
              console.log(data);
              });

              function CheckValues(value) {
              switch (value) {
              case "checkin":
              $(".select2-selection").addClass("defaultColor");
              break;
              case "notcheckin":
              $(".select2-selection").addClass("blueColor");
              break;
              case "cancel":
              $(".select2-selection").addClass("pinkColor");
              break;
              }
              }

              CheckValues($(".select2").val())

              });

              //With "not check-in": background color change to blue, and with "cancel": background color change to pink.

              .wrap-ds-chitiet+.select2-container {
              width: 100% !important;
              }

              .select2-search--dropdown {
              display: none !important;
              }

              .wrap-ds-chitiet+.select2-container .select2-selection {
              height: 55px;
              border-top-right-radius: 10px;
              border-top-left-radius: 10px;
              border: none;
              font-family: 'Montserrat';
              font-weight: 600;
              font-size: 0.93rem;
              color: #fff;
              }

              .wrap-ds-chitiet+.select2-container .select2-selection--single .select2-selection__rendered {
              color: #fff;
              padding-left: 20px;
              padding-top: 13px;
              }

              .wrap-ds-chitiet+.select2-container--default .select2-selection--single .select2-selection__arrow {
              margin-right: 21px;
              margin-top: 11px;
              }

              .default {
              background: linear-gradient(to right, #f8ca6b, #fbad7d);
              }

              .defaultColor {
              background: linear-gradient(to right, #f8ca6b, #fbad7d);
              }

              .select2-selection--single{
              background-color:transparent;
              }

              .blueColor {
              background: linear-gradient(to right, blue, blue);
              }

              .pinkColor {
              background: linear-gradient(to right, #FFC0CB, #FFC0CB);
              }

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
              <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>

              <div>
              <select class="form-control select2 wrap-ds-chitiet" id="ds-chitiet-option">
              <option value="checkin">Check-in</option>
              <option value="notcheckin">Not check-in</option>
              <option value="cancel">Cancel</option>
              </select>
              </div>








              share|improve this answer


























                0












                0








                0






                You can use select2:select event to achieve this,



                I have added values to option and created CheckValues common function to add the classes and css classes.



                Please check demo.






                $(document).ready(function() {
                $('.select2').select2()
                $('.select2').on('select2:select', function(e) {
                var data = e.params.data;
                $(".select2-selection").removeClass("defaultColor blueColor blueColor");
                CheckValues(data.element.value)
                console.log(data);
                });

                function CheckValues(value) {
                switch (value) {
                case "checkin":
                $(".select2-selection").addClass("defaultColor");
                break;
                case "notcheckin":
                $(".select2-selection").addClass("blueColor");
                break;
                case "cancel":
                $(".select2-selection").addClass("pinkColor");
                break;
                }
                }

                CheckValues($(".select2").val())

                });

                //With "not check-in": background color change to blue, and with "cancel": background color change to pink.

                .wrap-ds-chitiet+.select2-container {
                width: 100% !important;
                }

                .select2-search--dropdown {
                display: none !important;
                }

                .wrap-ds-chitiet+.select2-container .select2-selection {
                height: 55px;
                border-top-right-radius: 10px;
                border-top-left-radius: 10px;
                border: none;
                font-family: 'Montserrat';
                font-weight: 600;
                font-size: 0.93rem;
                color: #fff;
                }

                .wrap-ds-chitiet+.select2-container .select2-selection--single .select2-selection__rendered {
                color: #fff;
                padding-left: 20px;
                padding-top: 13px;
                }

                .wrap-ds-chitiet+.select2-container--default .select2-selection--single .select2-selection__arrow {
                margin-right: 21px;
                margin-top: 11px;
                }

                .default {
                background: linear-gradient(to right, #f8ca6b, #fbad7d);
                }

                .defaultColor {
                background: linear-gradient(to right, #f8ca6b, #fbad7d);
                }

                .select2-selection--single{
                background-color:transparent;
                }

                .blueColor {
                background: linear-gradient(to right, blue, blue);
                }

                .pinkColor {
                background: linear-gradient(to right, #FFC0CB, #FFC0CB);
                }

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
                <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>

                <div>
                <select class="form-control select2 wrap-ds-chitiet" id="ds-chitiet-option">
                <option value="checkin">Check-in</option>
                <option value="notcheckin">Not check-in</option>
                <option value="cancel">Cancel</option>
                </select>
                </div>








                share|improve this answer














                You can use select2:select event to achieve this,



                I have added values to option and created CheckValues common function to add the classes and css classes.



                Please check demo.






                $(document).ready(function() {
                $('.select2').select2()
                $('.select2').on('select2:select', function(e) {
                var data = e.params.data;
                $(".select2-selection").removeClass("defaultColor blueColor blueColor");
                CheckValues(data.element.value)
                console.log(data);
                });

                function CheckValues(value) {
                switch (value) {
                case "checkin":
                $(".select2-selection").addClass("defaultColor");
                break;
                case "notcheckin":
                $(".select2-selection").addClass("blueColor");
                break;
                case "cancel":
                $(".select2-selection").addClass("pinkColor");
                break;
                }
                }

                CheckValues($(".select2").val())

                });

                //With "not check-in": background color change to blue, and with "cancel": background color change to pink.

                .wrap-ds-chitiet+.select2-container {
                width: 100% !important;
                }

                .select2-search--dropdown {
                display: none !important;
                }

                .wrap-ds-chitiet+.select2-container .select2-selection {
                height: 55px;
                border-top-right-radius: 10px;
                border-top-left-radius: 10px;
                border: none;
                font-family: 'Montserrat';
                font-weight: 600;
                font-size: 0.93rem;
                color: #fff;
                }

                .wrap-ds-chitiet+.select2-container .select2-selection--single .select2-selection__rendered {
                color: #fff;
                padding-left: 20px;
                padding-top: 13px;
                }

                .wrap-ds-chitiet+.select2-container--default .select2-selection--single .select2-selection__arrow {
                margin-right: 21px;
                margin-top: 11px;
                }

                .default {
                background: linear-gradient(to right, #f8ca6b, #fbad7d);
                }

                .defaultColor {
                background: linear-gradient(to right, #f8ca6b, #fbad7d);
                }

                .select2-selection--single{
                background-color:transparent;
                }

                .blueColor {
                background: linear-gradient(to right, blue, blue);
                }

                .pinkColor {
                background: linear-gradient(to right, #FFC0CB, #FFC0CB);
                }

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
                <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>

                <div>
                <select class="form-control select2 wrap-ds-chitiet" id="ds-chitiet-option">
                <option value="checkin">Check-in</option>
                <option value="notcheckin">Not check-in</option>
                <option value="cancel">Cancel</option>
                </select>
                </div>








                $(document).ready(function() {
                $('.select2').select2()
                $('.select2').on('select2:select', function(e) {
                var data = e.params.data;
                $(".select2-selection").removeClass("defaultColor blueColor blueColor");
                CheckValues(data.element.value)
                console.log(data);
                });

                function CheckValues(value) {
                switch (value) {
                case "checkin":
                $(".select2-selection").addClass("defaultColor");
                break;
                case "notcheckin":
                $(".select2-selection").addClass("blueColor");
                break;
                case "cancel":
                $(".select2-selection").addClass("pinkColor");
                break;
                }
                }

                CheckValues($(".select2").val())

                });

                //With "not check-in": background color change to blue, and with "cancel": background color change to pink.

                .wrap-ds-chitiet+.select2-container {
                width: 100% !important;
                }

                .select2-search--dropdown {
                display: none !important;
                }

                .wrap-ds-chitiet+.select2-container .select2-selection {
                height: 55px;
                border-top-right-radius: 10px;
                border-top-left-radius: 10px;
                border: none;
                font-family: 'Montserrat';
                font-weight: 600;
                font-size: 0.93rem;
                color: #fff;
                }

                .wrap-ds-chitiet+.select2-container .select2-selection--single .select2-selection__rendered {
                color: #fff;
                padding-left: 20px;
                padding-top: 13px;
                }

                .wrap-ds-chitiet+.select2-container--default .select2-selection--single .select2-selection__arrow {
                margin-right: 21px;
                margin-top: 11px;
                }

                .default {
                background: linear-gradient(to right, #f8ca6b, #fbad7d);
                }

                .defaultColor {
                background: linear-gradient(to right, #f8ca6b, #fbad7d);
                }

                .select2-selection--single{
                background-color:transparent;
                }

                .blueColor {
                background: linear-gradient(to right, blue, blue);
                }

                .pinkColor {
                background: linear-gradient(to right, #FFC0CB, #FFC0CB);
                }

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
                <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>

                <div>
                <select class="form-control select2 wrap-ds-chitiet" id="ds-chitiet-option">
                <option value="checkin">Check-in</option>
                <option value="notcheckin">Not check-in</option>
                <option value="cancel">Cancel</option>
                </select>
                </div>





                $(document).ready(function() {
                $('.select2').select2()
                $('.select2').on('select2:select', function(e) {
                var data = e.params.data;
                $(".select2-selection").removeClass("defaultColor blueColor blueColor");
                CheckValues(data.element.value)
                console.log(data);
                });

                function CheckValues(value) {
                switch (value) {
                case "checkin":
                $(".select2-selection").addClass("defaultColor");
                break;
                case "notcheckin":
                $(".select2-selection").addClass("blueColor");
                break;
                case "cancel":
                $(".select2-selection").addClass("pinkColor");
                break;
                }
                }

                CheckValues($(".select2").val())

                });

                //With "not check-in": background color change to blue, and with "cancel": background color change to pink.

                .wrap-ds-chitiet+.select2-container {
                width: 100% !important;
                }

                .select2-search--dropdown {
                display: none !important;
                }

                .wrap-ds-chitiet+.select2-container .select2-selection {
                height: 55px;
                border-top-right-radius: 10px;
                border-top-left-radius: 10px;
                border: none;
                font-family: 'Montserrat';
                font-weight: 600;
                font-size: 0.93rem;
                color: #fff;
                }

                .wrap-ds-chitiet+.select2-container .select2-selection--single .select2-selection__rendered {
                color: #fff;
                padding-left: 20px;
                padding-top: 13px;
                }

                .wrap-ds-chitiet+.select2-container--default .select2-selection--single .select2-selection__arrow {
                margin-right: 21px;
                margin-top: 11px;
                }

                .default {
                background: linear-gradient(to right, #f8ca6b, #fbad7d);
                }

                .defaultColor {
                background: linear-gradient(to right, #f8ca6b, #fbad7d);
                }

                .select2-selection--single{
                background-color:transparent;
                }

                .blueColor {
                background: linear-gradient(to right, blue, blue);
                }

                .pinkColor {
                background: linear-gradient(to right, #FFC0CB, #FFC0CB);
                }

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css" rel="stylesheet" />
                <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js"></script>

                <div>
                <select class="form-control select2 wrap-ds-chitiet" id="ds-chitiet-option">
                <option value="checkin">Check-in</option>
                <option value="notcheckin">Not check-in</option>
                <option value="cancel">Cancel</option>
                </select>
                </div>






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 21 at 5:30

























                answered Nov 21 at 5:17









                Just code

                8,71642966




                8,71642966






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405493%2fhow-can-i-set-a-different-color-for-each-option-in-a-select2%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Wiesbaden

                    Marschland

                    Dieringhausen