I have an ordinary select box that I change to a fancy select2. Now, I want to apply a certain style attribute to it.
$('#myID').select2();
$('#myID').css('background-color','red');
This simple approach does not work. What is the solution?
I have an ordinary select box that I change to a fancy select2. Now, I want to apply a certain style attribute to it.
$('#myID').select2();
$('#myID').css('background-color','red');
This simple approach does not work. What is the solution?
I found the solution on my own:
$('#select2-'+myID+'-container').parent().css('background-color', 'red');
This has been answered before and has caught me out more than once, you want to be editing the Select2 css files as the styling of the select element will be over-written. Or create a new theme for it.
.select2-search { background-color: #00f; }
or for a specific ID
#myID.select2-search { background-color: #00f; }
That's because with the select2();
command you've transformed your #myID
to a select2 object and it no longer acts as a simple <select>
id!
Your jQuery should look something like this instead
$($('#myID').data('select2')).css('background-color', 'red');
(Can't unfortunately test it myself right now, as jsfiddle doesn't support select2)
I could apply style to select2 this way:
$('#MyID').siblings('.select2').children('.selection').children('.select2-selection').css('border-color', 'red')