Input with datalist grouping
Asked Answered
U

1

15

I do with select in two ways:

  1. With disabled option.
  2. With optgroup tag.

<select name="curso1" id="curso1">
    <option disabled="" style="font-size:12px; text-align:left; font-family:Arial Black">Group</option>
    <option value="1">Windows 8</option>
    <option value="8">lalalala</option>
</select>
<select>
    <optgroup label="Swedish Cars">
        <option value="volvo">Volvo</option>
        <option value="saab">Saab</option>
    </optgroup>
    <optgroup label="German Cars">
        <option value="mercedes">Mercedes</option>
        <option value="audi">Audi</option>
    </optgroup>
</select>

How to do this in a datalist?

How do group with the datalist?

Unmoved answered 23/10, 2015 at 20:34 Comment(0)
S
4

This looks neat on Chrome, but I don't know how it will render in other browsers.

enter image description here

<label>Choose a browser from this list:
<input list="browsers" name="myBrowser" /></label>
<datalist id="browsers">
  <option value="Brave">GroupA</option>
  <option value="Firefox"></option>
  <option value="Safari"></option>
  <option value="Opera">GroupB</option>
  <option value="Internet Explorer"></option>
  <option value="Microsoft Edge"></option>
</datalist>
Sayer answered 16/11, 2016 at 20:41 Comment(3)
In my testing, Chrome is the only browser that displays the value to the right.Briefcase
the display is a bit different on macos with chrome but it still works: i.sstatic.net/PQqvWm.png (safari doesn't show the groups)Embus
Safari shows the groups now (version 14.0) on MacOS.Contractor

© 2022 - 2025 — McMap. All rights reserved.