Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change background color of clicked option in duallistbox

Im using bootstrap-duallistbox

Currently, when users click an option from either box, the option background turns blue for a second then the option is moved to the other select box.

enter image description here

I would like to change the color from blue to something else.

What state is this exactly? CSS applied to option:active, option:hover, option:focus do not work to select this state and change the color.

I thought this might work, but it also failed.

select:-internal-list-box option:checked {
    color: #333 !important;
    background-color: #FFC894 !important;
}

Nor did:

select:-internal-list-box option:selected  {
    color: #333 !important;
    background-color: #FFC894 !important;
}

select:-internal-list-box:focus  option:checked  {
    color: #333 !important;
    background-color: #FFC894 !important;
}

select:-internal-list-box:focus  option:selected  {
    color: #333 !important;
    background-color: #FFC894 !important;
}

select:-internal-list-box:focus option::selection  {
    color: #333 !important;
    background-color: #FFC894 !important;
}

.bootstrap-duallistbox-container:focus select option::selection {
    background: #ffb7b7 !important;
}

How can I change the background color displayed when an option is clicked?

Here is a jsFiddle showing the issue

like image 488
Wesley Smith Avatar asked Oct 03 '15 11:10

Wesley Smith


People also ask

How do I change the background color on a selected option?

To change the selected option background-color CSS style, we can set the style attribute of the select and option elements. to set the whole select element to background color 009966 and color FFF . Then we override the the styles in the option elements with style="background: white; color: black;" .

How do I change the background color when I click the button?

Use HTML DOM Style backgroundColor Property to change the background color after clicking the button. This property is used to set the background-color of an element.

How do you change the background color of selected text in CSS?

You can change the background color and color of selected text by styling ::selection . Styling this pseudo element is great for matching user-selected text to your sites color scheme.

How do I select a background color in HTML?

To add background color in HTML, use the CSS background-color property. Set it to the color name or code you want and place it inside a style attribute. Then add this style attribute to an HTML element, like a table, heading, div, or span tag.


1 Answers

Turns out that to do this, you have to set the background property of the option not the background-color property like so:

var demo1 = $('select').bootstrapDualListbox();
select option:hover, 
select option:focus, 
select option:active, 
select option:checked
{
    background: linear-gradient(#FFC894,#FFC894);
    background-color: #FFC894 !important; /* for IE */
}
<link href="https://rawgit.com/istvan-ujjmeszaros/bootstrap-duallistbox/master/src/bootstrap-duallistbox.css" rel="stylesheet"/>
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script>
<script src="https://rawgit.com/istvan-ujjmeszaros/bootstrap-duallistbox/master/src/jquery.bootstrap-duallistbox.js"></script>
<select multiple="multiple" size="10" name="" class="no_selection">
    <option value="option1">Option 1</option>
    <option value="option2">Option 2</option>
    <option value="option3" selected="selected">Option 3</option>
    <option value="option4">Option 4</option>
    <option value="option5">Option 5</option>
    <option value="option6" selected="selected">Option 6</option>
    <option value="option7">Option 7</option>
    <option value="option8">Option 8</option>
    <option value="option9">Option 9</option>
    <option value="option0">Option 10</option>
</select>

Tested working in the following browsers:

Windows

  • Chrome 45.0.2454.101+
  • Fire Fox 36.0.4+
  • IE 10+

Mac

  • Chrome 45.0.2454.101+
  • Fire Fox 40.0.3+
  • Opera 30.0+

Ubuntu (thanks @davidkonrad)

  • Chrome
  • Fire Fox

Safari does see the property, it shows active in the inspector, but it somehow ignores it anyway.


My total guess at why this works

Using CSS multiple backgrounds states:

With CSS3, you can apply multiple backgrounds to elements. These are layered atop one another with the first background you provide on top and the last background listed in the back.

It seems to me that, the user agent blue background is still there, but the colored background added by background: linear-gradient(#FFC894,#FFC894); is layered on top of it.

like image 180
Wesley Smith Avatar answered Oct 03 '22 21:10

Wesley Smith