Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change background color select 2 using javascript?

Demo and my code is like this : http://jsfiddle.net/fc1qevem/10/

My javascript code is like this :

var select02 = $('#select02');

$(select02).select2({
    data: [{
        id: 0,
        text: "test01"
    }, {
        id: 1,
        text: "test02"
    }, {
        id: 2,
        text: "test03"
    }, {
        id: 3,
        text: "test04"
    }, {
        id: 4,
        text: "test05"
    }],
     placeholder: "Branch name",
});

I want change the background color of select2 to blue color

I want change the background color using javascript

Whether it can be done?

Thank you

like image 950
moses toh Avatar asked Apr 02 '26 15:04

moses toh


1 Answers

add this in your css

.select2-selection{
  background-color:blue !important;
}

js solution

 $(select02).select2({
    data: [{
        id: 0,
        text: "test01"
    }, {
        id: 1,
        text: "test02"
    }, {
        id: 2,
        text: "test03"
    }, {
        id: 3,
        text: "test04"
    }, {
        id: 4,
        text: "test05"
    }],
     placeholder: "Branch name",
}).data("select2").$container.find(".select2-selection").css('background-color', 'blue');
like image 114
Vladu Ionut Avatar answered Apr 04 '26 04:04

Vladu Ionut



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!