How to change the style of a select's optgroup label?
Unfortunately select boxes are one of the few things that you can add very little style to with CSS. You are usually limited to how the browser renders it.
For example, it looks like this in chrome:
And this in Firefox:
Styling option group label
Use attribute Selector
[label]
{
color: red;
}
EDIT
<select>
<optgroup label="Cars">
<option>Honda</option>
<option>Merc</option>
</optgroup>
<optgroup label="Bikes">
<option>Kawasaki</option>
<option>Yamaha</option>
</optgroup>
</select>
optgroup[label="Cars"]
{
color: red;
}
optgroup[label="Bikes"]
{
color: blue;
}
option
{
color: black;
}
styles on optgroup
Use the CSS selector select optgroup
instead.
Be aware that not every browser will respect optgroup
rules (Webkit, for example). With that in mind, you might like to try a jQuery plugin that allows complete, cross-browsing styling of form elements; such as Uniform.
Is there any way to style optgroup using CSS on the iPad?
Unfortunately, there isn't a way to do it. iPad Safari takes full control of styling select lists' internal contents. Here's a reference for verification: little link.
One way to achieve this this would be to simulate the dropdown/select menu using JavaScript. It's not very preferable, but if you require to change the default styling, then I'm afraid it's the only way to go; here's a demo that should give you an idea on how to do the simulation: another little link.
How to set selected value of jQuery Select2?
To dynamically set the "selected" value of a Select2 component:
$('#inputID').select2('data', {id: 100, a_key: 'Lorem Ipsum'});
Where the second parameter is an object with expected values.
UPDATE:
This does work, just wanted to note that in the new select2, "a_key" is "text" in a standard select2 object. so: {id: 100, text: 'Lorem Ipsum'}
Example:
$('#all_contacts').select2('data', {id: '123', text: 'res_data.primary_email'});
Thanks to @NoobishPro
Select2 v4.0 make optgroups selectable
This was requested over on GitHub, but realistically it is not actually possible. It was previously possible with a <input />
, but the switch to a <select>
meant that we now explicitly disallow a selectable <optgroup>
.
There is a technical barrier here that Select2 will likely never be able to tackle: A standard <optgroup>
is not selectable in the browser. And because a data object with children
is converted to an <optgroup>
with a set of nested <option>
elements, the problem applies in your case with the same issue.
The best possible solution is to have an <option>
for selecting the group, like you would have with a standard select. Without an <option>
tag, it's not possible to set the selected value to the group (as the value doesn't actually exist). Select2 even has a templateResult
option (formatResult
in 3.x) so you can style it as a group consistently across browsers.
Related Topics
File Opens Instead of Downloading in Internet Explorer in a Href Link
How to Remove The Dotted Line Around The Clicked a Element in HTML
How to Extract Data from HTML Table in Shell Script
How to Escape HTML-Specific Characters in a String (Powershell)
Vuejs/Browser Caching Production Builds
Ticks for Type="Range" HTML Input
HTML Embed Autoplay="False", But Still Plays Automatically
How to Remove The Border Around an Image Without a Source
How to Insert a Template into Another Template
Combining Two or More Canvas Elements with Some Sort of Blending
Should Tables Be Avoided in HTML at Any Cost
How to Define a Dynamic Mixin or Function Name in Sass
Content Security Policy: "Img-Src 'self' Data:"
What Does "Semantically Correct" Mean
Using Rvest or Httr to Log in to Non-Standard Forms on a Webpage