Optgroups in a datalist - html

I am trying to create a input-box where the available anwsers ar listed in a list below. This I can do but i am not able to sort the available anwsers in optgroups. When the code runs the anwsers is listed without the optgroups.
Is it at all possible to use optgroups in datalists or is optgroups exclusive to select elements?
Here is a code sample where the optgroups is not shown
<label>Välj en elbil
<input class="downdrop" id="valdModellCompare2" list="elfordon" onchange=getSelectValue();></label>
<datalist id="elfordon">
<optgroup value="Elbilar">elbil
<option value="teslas">Tesla Model S</option>
<option value="teslax">Tesla Model X</option>
<option value="tesla3">Tesla Model 3</option>
<option value="i3">BMW i3</option>
<option value="id3">Volkswagen ID.3</option>
<option value="id4">Volkswagen ID.4</option>
<option value="niro">KIA Niro</option>
<option value="etron">Audi e-Tron</option>
<option value="polestar">Polestar 2</option>
<option value="xc40e">Volvo XC40 Recharge</option>
<option value="leaf">Nissan Leaf</option>
<option value="taycan">Porsche Taycan</option>
</optgroup>
<optgroup value="Plug-in Hybrider">hybrid
<option value="outlander">Mitsubitshi Outlander Hybrid</option>
<option value="v60hybrid">Volvo V60 twin engine</option>
<option value="optima">KIA Optima SV</option>
<option value="passatgte">Volkswagen Passat GTE</option>
<option value="priushybrid">Toyota Prius Hybrid</option>
<option value="golfgte">Volkswagen Golf GTE</option>
<option value="x545e">BMW X5 45e</option>
<option value="530e">BMW 530e</option>
</optgroup>
</datalist>

As the documentation says for <optgroup> :
Permitted parents: A <select> element.
There are no other elements mentioned as permitted parents. Therefore optgroup is exclusively a child of select.

While optgroups is exclusively a child of select, select can be a child of datalist!
Although it is not the cleanest solution, you can use a trick used for compatibility of browsers that don't support datalist https://html.spec.whatwg.org/multipage/form-elements.html#the-datalist-element
In the more elaborate case, the datalist element can be given contents that are to be displayed for down-level clients that don't support datalist. In this case, the option elements are provided inside a select element inside the datalist element.
With the addition of value="" readonly to an option displaying the optgroup name. So that it is not possbile to select it.
<label>Välj en elbil
<input class="downdrop" id="valdModellCompare2" list="elfordon" onchange=getSelectValue();></label>
<datalist id="elfordon">
<label> Select from the list:
<select name="car">
<optgroup value="Elbilar">elbil
<option value="" readonly>Elbilar</option>
<option value="teslas"> >Tesla Model S</option>
<option value="teslax"> >Tesla Model X</option>
<option value="tesla3"> >Tesla Model 3</option>
<option value="i3"> >BMW i3</option>
</optgroup>
<optgroup value="Plug-in Hybrider">hybrid
<option value="" readonly>Plug-in Hybrider</option>
<option value="outlander"> >Mitsubitshi Outlander Hybrid</option>
<option value="v60hybrid"> >Volvo V60 twin engine</option>
<option value="optima"> >KIA Optima SV</option>
</optgroup>
</select>
</label>
</datalist>

Related

HTML select options are inaccessible

I must have missed something very trivial, but I cant figure it out.
The select below shows 5 options (as expected), but I cant get access to option 6 and 7. The scrollbar is shown, but the browser doesnt allow me to move it down
<select size='5' multiple name='driver[]'>
<option value= >(No Driver Filter)</option>
<option value=drivername=''></option>
<option value=drivername='alex'>alex</option>
<option value=drivername='marc'>marc</option>
<option value=drivername='frank'>frank</option>
<option value=drivername='james'>james</option>
<option value=drivername='michael'>michael</option>
</select>
I think your code is not well formatted . You should use double quotes for attribute values.
<select size="5" multiple="multiple" name="driver[]">
<option value="" >(No Driver Filter)</option>
<option value="drivername=''"></option>
<option value="drivername='alex'">alex</option>
<option value="drivername='marc'">marc</option>
<option value="drivername='frank'">frank</option>
<option value="drivername='james'">james</option>
<option value="drivername='michael'">michael</option>
</select>

how to put multiple values in an option?

I have a select with values like van, economy, economy-citroen. I select the van option and it displays the vans, however I would like to make an option that calls the van and economy values at the same time (not the multiple select version).
Something like value="economy van".
<select id="car_type" class="car_type span12">
<option value="-1" selected="selected">Any</option>
<option value="economy">Economy</option>
<option value="economy-citroen">Economy</option>
<option value="premium">Premium</option>
<option value="standard">Standard</option>
<option value="van">Van</option>
</select>
I would like to make it like:
<select id="car_type" class="car_type span12">
<option value="-1" selected="selected">Any</option>
<option value="economy">Economy</option>
<option value="economy-citroen">Economy</option>
<option value="premium">Premium</option>
<option value="standard">Standard</option>
<option value="van">Van</option>
<option value="van economy">Economy Van</option> ---so it displays both the economy and vans
</select>
Use this
<select multiple>
<!-- options here -->
</select>
Using this, you'll be able to select multiple options for that select element.
To read more on the HTML element select please refer the Mozilla Developer Network's document
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select
Add the multiple attribute to the select element.

How to add a title to a html select tag

How would I go about setting a title in select tag? Here is my select box:
<select>
<option value="sydney">Sydney</option>
<option value="melbourne">Melbourne</option>
<option value="cromwell">Cromwell</option>
<option value="queenstown">Queenstown</option>
</select>
When I visit the site, by default it shows "Sydney". But I want to display a title, such as, "What is the name of your city?"
<select>
<option selected disabled>Choose one</option>
<option value="sydney">Sydney</option>
<option value="melbourne">Melbourne</option>
<option value="cromwell">Cromwell</option>
<option value="queenstown">Queenstown</option>
</select>
Using selected and disabled will make "Choose one" be the default selected value, but also make it impossible for the user to actually select the item, like so:
<select>
<optgroup label = "Choose One">
<option value ="sydney">Sydney</option>
<option value ="melbourne">Melbourne</option>
<option value ="cromwell">Cromwell</option>
<option value ="queenstown">Queenstown</option>
</optgroup>
</select>
You can combine it with selected and hidden
<select class="dropdown" style="width: 150px; height: 26px">
<option selected hidden>What is your name?</option>
<option value="michel">Michel</option>
<option value="thiago">Thiago</option>
<option value="Jonson">Jonson</option>
</select>
Your dropdown title will be selected and cannot chose by the user.
You can use the following
<select data-hai="whatup">
<option label="Select your city">Select your city</option>
<option value="sydney">Sydney</option>
<option value="melbourne">Melbourne</option>
<option value="cromwell">Cromwell</option>
<option value="queenstown">Queenstown</option>
</select>
I think that this would help:
<select name="select_1">
<optgroup label="First optgroup category">
<option selected="selected" value="0">Select element</option>
<option value="2">Option 1</option>
<option value="3">Option 2</option>
<option value="4">Option 3</option>
</optgroup>
<optgroup label="Second optgroup category">
<option value="5">Option 4</option>
<option value="6">Option 5</option>
<option value="7">Option 6</option>
</optgroup>
</select>
<option value="" selected style="display:none">Please select one item</option>
Using selected and using display: none; for hidden item in list.
You can add an option tag on top of the others with no value and a prompt like this:
<select>
<option value="">Choose One</option>
<option value ="sydney">Sydney</option>
<option value ="melbourne">Melbourne</option>
<option value ="cromwell">Cromwell</option>
<option value ="queenstown">Queenstown</option>
</select>
Or leave it blank instead of saying Choose one if you want.
Typically, I would suggest that you use the <optgroup> option, as that gives some nice styling and indenting to the element.
The HTML element creates a grouping of options within a element. (Source: MDN Web Docs: <optgroup>.
But, since an <optgroup> cannot be a selected value, you can make an <option selected disabled> and then stylize it with CSS so that it behaves like an <optgroup>....
.optionGroup {
font-weight: bold;
font-style: italic;
}
<select>
<option class="optionGroup" selected disabled>Choose one</option>
<option value="sydney" class="optionChild"> Sydney</option>
<option value="melbourne" class="optionChild"> Melbourne</option>
<option value="cromwell" class="optionChild"> Cromwell</option>
<option value="queenstown" class="optionChild"> Queenstown</option>
</select>
With a default option having selected attribute
<select>
<option value="" selected>Choose your city</option>
<option value ="sydney">Sydney</option>
<option value ="melbourne">Melbourne</option>
<option value ="cromwell">Cromwell</option>
<option value ="queenstown">Queenstown</option>
</select>
The first option's text will always display as default title.
<select>
<option value ="">What is the name of your city?</option>
<option value ="sydney">Sydney</option>
<option value ="melbourne">Melbourne</option>
<option value ="cromwell">Cromwell</option>
<option value ="queenstown">Queenstown</option>
</select>
You can create dropdown title | label with selected, hidden and style for old or unsupported device.
<select name="city" >
<option selected hidden style="display:none">What is your city</option>
<option value="1">Sydney</option>
<option value="2">Melbourne</option>
<option value="3">Cromwell</option>
<option value="4">Queenstown</option>
</select>
I added a <div> and it worked fine
<div title="Your title here!">
<select>
<option value="sydney">Sydney</option>
<option value="melbourne">Melbourne</option>
<option value="cromwell">Cromwell</option>
<option value="queenstown">Queenstown</option>
</select>
</div>
<select name="city">
<option value ="0">What is your city</option>
<option value ="1">Sydney</option>
<option value ="2">Melbourne</option>
<option value ="3">Cromwell</option>
<option value ="4">Queenstown</option>
</select>
You can use the unique id for the value instead

Second HTML option load by default

In an HTML select element, is it possible to have the second option selected by default?
<select class="styled" onchange="location = this.options[this.selectedIndex].value;">
<option value="9u.html">Team 9u</option>
<option value="10u.html">Team 10u</option>
<option value="11u.html">Team 11u</option>
<option value="12u.html">Team 12u</option>
<option value="13u.html">Team 13u</option>
<option value="14u.html">Team 14u</option>
<option value="15u.html">Team 15u</option>
<option value="16u.html">Team 16u</option>
</select>
I would like the value "10u" to be in the select box when I load the page.
Use the selected attribute. Is google down?
<option value="10u.html" selected>Team 10u</option>

How to add Option group in HTML select?

How can I add option group in HTML select tag?
I want to categories my option list with option group....how can I use it?
Here is an example
<select>
<!-- First category option -->
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<!-- Second catgory option -->
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
By using the optgroup tag. Here is an example:
<select name="browser">
<optgroup label="Firefox">
<option value="2.0 or higher">
Firefox 2.0 or higher
</option>
<option value="1.5.x">Firefox 1.5.x</option>
<option value="1.0.x">Firefox 1.0.x</option>
</optgroup>
<optgroup label="Microsoft Internet Explorer">
<option value="7.0 or higher">
Microsoft Internet Explorer 7.0 or higher
</option>
<option value="6.x">Microsoft Internet Explorer 6.x</option>
<option value="5.x">Microsoft Internet Explorer 5.x</option>
<option value="4.x">Microsoft Internet Explorer 4.x</option>
</optgroup>
<optgroup label="Opera">
<option value="9.0 or higher">Opera 9.0 or higher</option>
<option value="8.x">Opera 8.x</option>
<option value="7.x">Opera 7.x</option>
</optgroup>
<option>Safari</option>
<option>Other</option>
</select>
hope it helps.
You are looking for the <optgroup> tag in which you should nest your options.
Optgroup is what you are looking for.
Use select option in optgroup as discuss above.
But I will show how to group select in Angular in *ngFor loop
<select class="form-control">
<option value=null disabled selected>Select</option>
<optgroup label="Active">
<option *ngFor="let type of userInformationTypes" [value]="type.value">{{type.text}}</option>
</optgroup>
<optgroup label="Inactive">
<option *ngFor="let type of inActiveUserInformation" [value]="type.value">{{type.text}}</option>
</optgroup>
</select>