最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How to change background color select 2 using javascript? - Stack Overflow

programmeradmin0浏览0评论

Demo and my code is like this : /

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

Demo and my code is like this : http://jsfiddle/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

Share Improve this question asked May 23, 2016 at 9:39 moses tohmoses toh 13.2k81 gold badges265 silver badges459 bronze badges 3
  • .select2-search input{ background-color: blue; } try this in your css – user2042292 Commented May 23, 2016 at 9:46
  • @Karthikeyan, If css can be placed in javascript? – moses toh Commented May 23, 2016 at 10:04
  • $('.select2-selection').css('background-color', 'blue'); @moses t you can try this. – user2042292 Commented May 23, 2016 at 10:06
Add a ment  | 

5 Answers 5

Reset to default 2

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');

I use it this way and it works very well:

$("#select02").siblings('.select2').children('.selection').children('.select2-selection').css('background-color', 'blue')

When you will change the background-color using JavaScript try this:

$('.select2-selection').css('background-color', 'blue');

// Css code
.bg_color {
     background-color: blue !important;
}

// Javascript code
// just put this code under the load function

$(select02).select2({ containerCssClass: "bg_color " });

This is how select2 adding class to the container or default css ".select2-container--default .select2-selection--single" of select2 where you can edit the background color of the container. Hope it helps you.

Try the code below:-

document.getElementById("select02").style.backgroundColor="blue";
发布评论

评论列表(0)

  1. 暂无评论