How do I give styling to select placeholder when it is disabled? - html

I have a requirement in my project to give a particular type of styling to the default i.e. placeholder value of a select tag when the select is "disabled".
I am aware that it can be given using select:invalid but I find it working only when select is "required" and but I want a solution for the below scenario.
<select disabled>
<option disabled value="" selected>- please choose -</option>
<option value="1">Apple</option>
<option value="2">Banana</option>
</select>
Note:
The requirement I have to resolve is in the select box the placeholder option i.e. the disabled option "-please choose-" has to be in italics and the rest of the options have to be in normal font style. Even when the select box is expanded. so you see in the "required state" I am able to give them different styles using :invalid selector(which doesn't works when the select is in "disabled state"), but in the disabled state, I am able to give just one type i..e italics or normal, if an option is selected and the select box is disabled/made non-editable following some condition, the option is looking like it is a placeholder because of the italics style.

This should work :
select:disabled {
background: red; /* For exemple */
}

I tried my best understanding your problem statement and hence tried to cover the explanatory scenarios which might work for you , please go through the developer comments in both the html and css files.
select:invalid,
select:disabled{
border-color: red;
}
/*
these are the default values which select disabled attribute adds to the CSS of select box by default
opacity: 0.7;
border-color: rgba(118, 118, 118, 0.3);
*/
<!--Scenario 1: Where you had only the required attribute and wanted to validate it by say border red-->
<select required>
<option disabled value="" selected>- please choose -</option>
<option value="1">Apple</option>
<option value="2">Banana</option>
</select>
<!--Scenario 2: where your selectbox is disabled yet shows a required type error-->
<select disabled>
<option disabled value="" selected>- please choose -</option>
<option value="1">Apple</option>
<option value="2">Banana</option>
</select>

Please use this CSS approach in all condition of select menu :-
select{color:pink;}
select option{color:blue;}
select option[disabled]{color: red; font-style:italic;}
select:disabled{color: yellow; font-style:italic;}
<select disabled>
<option disabled value="" selected>- please choose -</option>
<option value="1">Apple</option>
<option value="2">Banana</option>
</select>
<select>
<option disabled value="" selected>- please choose -</option>
<option value="1">Apple</option>
<option value="2">Banana</option>
</select>

try this It's working for me
<select class="form-control">
<option value="" readonly="true" hidden="true" selected>Select your option</option>
<option value="1">Something</option>
<option value="2">Something else</option>
<option value="3">Another choice</option>
</select>

Related

On OSX a <select> element always has first option checked by default [duplicate]

I have a very weird requirement, wherein I am required to have no option selected by default in drop down menu in HTML. However,
I cannot use this,
<select>
<option></option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
Because, for this I will have to do validation to handle the first option. Can anyone help me in achieving this target without actually including the first option as part of the select tag?
Maybe this will be helpful
<select>
<option disabled selected value> -- select an option -- </option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
-- select an option -- Will be displayed by default. But if you choose an option, you will not be able to select it back.
You can also hide it using by adding an empty option
<option style="display:none">
so it won't show up in the list anymore.
Option 2
If you don't want to write CSS and expect the same behaviour of the solution above, just use:
<option hidden disabled selected value> -- select an option -- </option>
You could use Javascript to achieve this. Try the following code:
HTML
<select id="myDropdown">
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
JS
document.getElementById("myDropdown").selectedIndex = -1;
or JQuery
$("#myDropdown").prop("selectedIndex", -1);
Today (2015-02-25)
This is valid HTML5 and sends a blank (not a space) to the server:
<option label=" "></option>
Verified validity on http://validator.w3.org/check
Verified behavior with Win7(IE11 IE10 IE9 IE8 FF35 Safari5.1) Ubuntu14.10(Chrome40, FF35) OSX_Yosemite(Safari8, Chrome40) Android(Samsung-Galaxy-S5)
The following also passes validation today, but passes some sort of space character to the server from most browsers (probably not desirable) and a blank on others (Chrome40/Linux passes a blank):
<option> </option>
Previously (2013-08-02)
According to my notes, the non-breaking-space entity inside the option tags shown above produced the following error in 2013:
Error: W3C Markup Validaton Service (Public): The first child option
element of a select element with a required attribute and without a
multiple attribute, and whose size is 1, must have either an empty
value attribute, or must have no text content.
At that time, a regular space was valid XHTML4 and sent a blank (not a space) to the server from every browser:
<option> </option>
Future
It would make my heart glad if the spec was updated to explicitly allow a blank option. Preferably using the briefest syntax. Either of the following would be great:
<option />
<option></option>
Test File
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Test</title>
</head>
<body>
<form action="index.html" method="post">
<select name="sel">
<option label=" "></option>
</select>
</form>
</body>
</html>
<td><b>Field Label:</b><br>
<select style='align:left; width:100%;' id='some_id' name='some_name'>
<option hidden selected>Select one...</option>
<option value='Value1'>OptLabel1</option>
<option value='Value2'>OptLabel2</option>
<option value='Value3'>OptLabel3</option></select>
</td>
Just put "hidden" on option you want to hide on dropdown list.
Solution that works by only using CSS:
A: Inline CSS
<select>
<option style="display:none;"></option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
B: CSS Style Sheet
If you have a CSS file at hand, you can target the first option using:
select.first-opt-hidden option:first-of-type {
display:none;
}
<select class="first-opt-hidden">
<option></option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
This should help:
https://www.w3schools.com/tags/att_select_required.asp
<form>
<select required>
<option value="">None</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
<button type="submit">Submit</button>
</form>
Just a small remark:
some Safari browsers do not seem to respect neither the "hidden" attribute nor the style setting "display:none" (tested with Safari 12.1 under MacOS 10.12.6). Without an explicit placeholder text, these browsers simply show an empty first line in the list of options. It may therefore be useful to always provide some explanatory text for this "dummy" entry:
<option hidden disabled selected value>(select an option)</option>
Thanks to the "disabled" attribute, it won't be actively selected anyway.
<select required>
<option value="" disabled selected>None</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
You can avoid custom validation in this case.
I understand what you are trying to do.The best and the most successful way is :
<select name='department' required>
<option value="">None</option>
<option value="Teaching">Teaching department</option>
<option value="nonTeaching">Non-teaching department</option>
</select>
I found it really interesting because I just experienced the same thing not so long time ago.
However, I came across to an example on the Internet about the solution regarding this.
Without any further ado, see the code fragment below:
<select>
<option value data-isdefault="true">--Choose one Option--</option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
With that, it will stay un-submittable but selectable, anytime. More convenience for User Interface and great for User Experience.
Well that's all, I hope it helps. Cheers!
There is no HTML solution. By the HTML 4.01 spec, browser behavior is undefined if none of the option elements has the selected attribute, and what browsers do in practice is that they make the first option pre-selected.
As a workaround, you could replace the select element by a set of input type=radio elements (with the same name attribute). This creates a control of the same kind though with different appearance and user interface. If none of the input type=radio elements has the checked attribute, none of them is initially selected in most modern browsers.
I'm using Laravel 5 framework and #Gambi `s answer worked for me as well but with some changes for my project.
I have the option values in a database table and I use them with a foreach statement. But before the statement I have added an option with #Gambit suggested settings and it worked.
Here my exemple:
#isset($keys)
<select>
<option disabled selected value></option>
#foreach($keys as $key)
<option>{{$key->value)</option>
#endforeach
</select>
#endisset
I hope this helps someone as well. Keep up the good work!
Try this:
<h2>Favorite color</h2>
<select name="color">
<option value=""></option>
<option>Pink</option>
<option>Red</option>
<option>Blue</option>
</select>
The first option in the drop down would be blank.
In order to show please select a value in drop down and hide it after some value is selected . please use the below code.
it will also support required validation.
<select class="form-control" required>
<option disabled selected value style="display:none;">--Please select a value</option>
<option >Data 1</option>
<option >Data 2</option>
<option >Data 3</option>
</select>
If you are using Angular (2+), (or any other framework), you could add some logic. The logic would be: only display an empty option if the user did not select any other yet.
So after the user selected an option, the empty option disappears.
For Angular (9) this would look something like this:
<select>
<option *ngIf="(hasOptionSelected$ | async) === false"></option>
<option *ngFor="let option of (options$ | async)[value]="option.id">{{ option.title }}</option>
</select>
For those who are using <select multiple> (combobox; no dropdown), this worked for me:
<select size=1 disabled multiple>
<option hidden selected></option>
<option>My Option</option>
</select>
If you don't need any empty option at first, try this first line:
<option style="display:none"></option>
just use "..option hidden selected.." as default option
I guess a good idea would be to use the radio buttons, set #1 as default and hide it, give it for example a
name="init" and a value="null" or whatever, up to you!
this way the radio buttons list has a value definitely, but default of null can be used logically!
I think it's not necessary to elaborate further, since the idea can easily be implemented with display: none; or visibility: hidden;
... whereas I think the first one display: none; is the better option:
In react, you can give a dummy value (say -1) with select tag as below and same value can be used with this disabled option of yours. (WORKED FOR ME)
const nonEmpty = selected[identifierField] || false;
<select
onChange={(e) => {
onSelect(
options.find((option) => option[identifierField] === e.target.value)
);
}}
value={nonEmpty || -1}
>
<option disabled value={-1}>Select Option</option>
{options.map((option) => (
<option key={option[identifierField]} value={option[identifierField]}>
{option[displayField]}
</option>
))}
</select>
option style="display:none"
Is bad solution for Tablet: iPad Pro / iOS 15 / Safari
An unnecessary row in the dropdown appears, only for real devices. Doesn`t reproduce on the emulator.
Try this:
<select>
<option value="">
<option>Option 1
<option>Option 2
<option>Option 3
</select>
Validates in HTML5. Works with required attribute in select element. Can be re-selected. Works in Google Chrome 45, Internet Explorer 11, Edge, Firefox 41.

How to Select all options in a dropdown by default using <Option Value=""> in chrome?

Here is the code I am having issues with:
<select name="webmenu" id="filter_option" style="width: 175px; " onchange="searchTree($(this).val());">
<option value="">--View All--</option>
<option value="Tree">Tree</option>
<option value="Dog Approval">Dog Approval</option>
<option value="Cat">Cat</option>
<option value="Sky">Sky</option>
I have an issue with the dropdown in that when I select this: <option value="">--View All--</option> in chrome the list does not load all default values when this option is selected.
When I go into chrome inspector and look at the line: <option value="">--View All--</option> the option value looks like this: <option>--View All--</option>
Why is it stripping out the value="" part of the code?
without the code ot link to that site I cannot guess what exactly is happening at your side.
but answering the question from the title…
you can only select multiple options if the attribute multiple is present in the tag select. then to make an option tag selected it needs the attribute selected.
so to select all options, all option tags need an attribute selected.
read up: select, multiple, option
<!doctype html>
<html>
<head><title></title></head>
<body>
<select size="5" multiple>
<option selected value="1">1</option>
<option selected value="2">2</option>
<option selected value="3">3</option>
<option selected value="4">4</option>
<option selected value="5">5</option>
</select>
</body></html>
if you can maybe add your problem to the above playground and a solution might be found through that.

hide selected value of a select list, when list is open

I have a dropdown select list, from which I can choose a country. the first option is: choose your country.
<select>
<option selected disabled value=''>Choose your country</option>
<option value="1">Canada</option>
<option value="2">France</option>
</select>
But when I click to open the list, I can see choose your country in the options. Is there a way to hide this option from the list, and only show it as a placeholder before any selection is made?
Hides the default option from appearing in the list:
http://jsfiddle.net/ZT2ZV/
IMO the better solution would be to use something like: http://harvesthq.github.io/chosen/
<select>
<option value='' disabled selected style='display:none;'>Choose your country</option>
<option value="1">Canada</option>
<option value="2">France</option>
</select>
Use display:none; on your first value:
<select>
<option value="" selected disabled style="display:none;">Choose your country</option>
<option value="2">Canada</option>
<option value="3">France</option>
</select>
try http://jsfiddle.net/uaF93/

<select> <option disabled> makes option item disappear in firefox

<select class="form-control" id="service_select">
<option disabled="disabled"></option>
</select>
In chrome it is fine. Is there another way to make it disabled but not hidden ? Thanks.
The option isn't hidden - you just don't have any text in it:
<select class="form-control" id="service_select">
<option disabled="disabled">Place some text here</option>
</select>
If your first option is disabled (and therefore cannot be selected) Firefox will select the first non-disabled option.
<select class="form-control" id="service_select">
<option disabled="disabled">One</option>
<option disabled="disabled">Two</option>
<option>Three</option>
</select>
The first two options can be seen when you inspect the drop-down list, but cannot be selected - so "Three" would be selected in the above example.
If you want to prevent the user from selecting the first option, but want to display it - use the required attribute with an empty value...
<select required>
<option value="">Please select</option>
<option value="1">One</option>
<option value="2">Two</option>
</select>

display:none doesn't work for option

Demo here
HTML:
display:none <b>not works</b>,the hidden can <b>not select</b>.<br>
<select size="5">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
<option value="E">E</option>
<option value="F">F</option>
<option value="G">G</option>
<option value="H">H</option>
<option value="I">I</option>
</select><br>
display:none <b>works</b>,the hidden <b>can select</b>.<br>
<select>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
<option value="E">E</option>
<option value="F">F</option>
<option value="G">G</option>
<option value="H">H</option>
<option value="I">I</option>
</select>
CSS:
select{width:50px;}
[value=C]{
display: none;
}
/* will hold the position */
[value=B]{
visibility: hidden;
}
The size attribute will affect the display and visibility, what happen to this ?
How can I hide the option in select which has a size attribute ?
See updated section
I think you can not do that only with CSS for all browsers you'll need some JS code, there is a previous question quite similar:
How to hide a <option> in a <select> menu with CSS?
In Chrome (v. 30) "display:none" doesn't work, however in Firefox (v. 24) It works, the option with "display:none" doesn't appear in the list.
UPDATE2:
In the current Chrome (v. 70) an Firefox (v. 63) versions, the use of css with "display:none" along with attribute "disabled" in the option tag removes the option from the list and it doesn't appear any more.
<html><body>
<select>
<option disabled style="display:none">Hola</option>
<option>Hello</option>
<option>Ciao</option>
</select>
</body></html>
Thanks to #achecopar for the help
The property Display:none wont work on the options tag
so you have only two options as work around
1. Either disable then with disabled="disabled".
2. Remove the options you don't want to see and insert them again when needed.
you may be able to find some other work around too, but i don't think it will be consistent in all the browsers
There is a technique for hiding options within a select in this post: How to hide a <option> in a <select> menu with CSS?
Use following jQuery to hide and show under select
jQuery(selector).toggleOption(true); // show option
jQuery(selector).toggleOption(false); // hide option
is you need this...
<select>
<option value="A">A</option>
<option disabled value="B">B</option>
<option value="C">C</option>
<option disabled value="D">D</option>
<option value="E">E</option>
<option value="F">F</option>
<option value="G">G</option>
<option value="H">H</option>
<option value="I">I</option>
</select>
the disable value are not select-able.
if you want to hide go here..
http://jsbin.com/anoci