Cara menggunakan select option attribute javascript

Di dalam suatu daftar pilihan kadang berisi butir pilihan yang sudah dikelompokkan berdasarkan kriteria tertentu. Untuk mengelompokkan butir-butir pilihan tersebut anda bisa menggunakan elemen HTML bersama dengan elemen

Show

    The above example shows typical <select> usage. It is given an id attribute to enable it to be associated with a <label> for accessibility purposes, as well as a name attribute to represent the name of the associated data point submitted to the server. Each menu option is defined by an

    <label>Please choose one or more pets:
      <select name="pets" multiple size="4">
        <optgroup label="4-legged pets">
          <option value="dog">Dog</option>
          <option value="cat">Cat</option>
          <option value="hamster" disabled>Hamster</option>
        </optgroup>
        <optgroup label="Flying pets">
          <option value="parrot">Parrot</option>
          <option value="macaw">Macaw</option>
          <option value="albatross">Albatross</option>
        </optgroup>
      </select>
    </label>
    
    0 element nested inside the <select>.

    Each

    <label>Please choose one or more pets:
      <select name="pets" multiple size="4">
        <optgroup label="4-legged pets">
          <option value="dog">Dog</option>
          <option value="cat">Cat</option>
          <option value="hamster" disabled>Hamster</option>
        </optgroup>
        <optgroup label="Flying pets">
          <option value="parrot">Parrot</option>
          <option value="macaw">Macaw</option>
          <option value="albatross">Albatross</option>
        </optgroup>
      </select>
    </label>
    
    0 element should have a attribute containing the data value to submit to the server when that option is selected. If no
    <label>Please choose one or more pets:
      <select name="pets" multiple size="4">
        <optgroup label="4-legged pets">
          <option value="dog">Dog</option>
          <option value="cat">Cat</option>
          <option value="hamster" disabled>Hamster</option>
        </optgroup>
        <optgroup label="Flying pets">
          <option value="parrot">Parrot</option>
          <option value="macaw">Macaw</option>
          <option value="albatross">Albatross</option>
        </optgroup>
      </select>
    </label>
    
    3 attribute is included, the value defaults to the text contained inside the element. You can include a attribute on an
    <label>Please choose one or more pets:
      <select name="pets" multiple size="4">
        <optgroup label="4-legged pets">
          <option value="dog">Dog</option>
          <option value="cat">Cat</option>
          <option value="hamster" disabled>Hamster</option>
        </optgroup>
        <optgroup label="Flying pets">
          <option value="parrot">Parrot</option>
          <option value="macaw">Macaw</option>
          <option value="albatross">Albatross</option>
        </optgroup>
      </select>
    </label>
    
    0 element to make it selected by default when the page first loads.

    The <select> element has some unique attributes you can use to control it, such as

    <label>Please choose one or more pets:
      <select name="pets" multiple size="4">
        <optgroup label="4-legged pets">
          <option value="dog">Dog</option>
          <option value="cat">Cat</option>
          <option value="hamster" disabled>Hamster</option>
        </optgroup>
        <optgroup label="Flying pets">
          <option value="parrot">Parrot</option>
          <option value="macaw">Macaw</option>
          <option value="albatross">Albatross</option>
        </optgroup>
      </select>
    </label>
    
    8 to specify whether multiple options can be selected, and
    <label>Please choose one or more pets:
      <select name="pets" multiple size="4">
        <optgroup label="4-legged pets">
          <option value="dog">Dog</option>
          <option value="cat">Cat</option>
          <option value="hamster" disabled>Hamster</option>
        </optgroup>
        <optgroup label="Flying pets">
          <option value="parrot">Parrot</option>
          <option value="macaw">Macaw</option>
          <option value="albatross">Albatross</option>
        </optgroup>
      </select>
    </label>
    
    9 to specify how many options should be shown at once. It also accepts most of the general form input attributes such as
    <form>
      <fieldset>
        <legend>Standard controls</legend>
        <select name="1A" id="select" autocomplete="off" required>
          <option>Carrots</option>
          <option>Peas</option>
          <option>Beans</option>
          <option>Pneumonoultramicroscopicsilicovolcanoconiosis</option>
        </select>
      </fieldset>
      <fieldset id="custom">
        <legend>Custom controls</legend>
        <select name="2A" id="select" autocomplete="off" required>
          <option>Carrots</option>
          <option>Peas</option>
          <option>Beans</option>
          <option>Pneumonoultramicroscopicsilicovolcanoconiosis</option>
        </select>
      </fieldset>
    </form>
    
    0,
    <form>
      <fieldset>
        <legend>Standard controls</legend>
        <select name="1A" id="select" autocomplete="off" required>
          <option>Carrots</option>
          <option>Peas</option>
          <option>Beans</option>
          <option>Pneumonoultramicroscopicsilicovolcanoconiosis</option>
        </select>
      </fieldset>
      <fieldset id="custom">
        <legend>Custom controls</legend>
        <select name="2A" id="select" autocomplete="off" required>
          <option>Carrots</option>
          <option>Peas</option>
          <option>Beans</option>
          <option>Pneumonoultramicroscopicsilicovolcanoconiosis</option>
        </select>
      </fieldset>
    </form>
    
    1,
    <form>
      <fieldset>
        <legend>Standard controls</legend>
        <select name="1A" id="select" autocomplete="off" required>
          <option>Carrots</option>
          <option>Peas</option>
          <option>Beans</option>
          <option>Pneumonoultramicroscopicsilicovolcanoconiosis</option>
        </select>
      </fieldset>
      <fieldset id="custom">
        <legend>Custom controls</legend>
        <select name="2A" id="select" autocomplete="off" required>
          <option>Carrots</option>
          <option>Peas</option>
          <option>Beans</option>
          <option>Pneumonoultramicroscopicsilicovolcanoconiosis</option>
        </select>
      </fieldset>
    </form>
    
    2, etc.

    You can further nest

    <label>Please choose one or more pets:
      <select name="pets" multiple size="4">
        <optgroup label="4-legged pets">
          <option value="dog">Dog</option>
          <option value="cat">Cat</option>
          <option value="hamster" disabled>Hamster</option>
        </optgroup>
        <optgroup label="Flying pets">
          <option value="parrot">Parrot</option>
          <option value="macaw">Macaw</option>
          <option value="albatross">Albatross</option>
        </optgroup>
      </select>
    </label>
    
    0 elements inside
    <form>
      <fieldset>
        <legend>Standard controls</legend>
        <select name="1A" id="select" autocomplete="off" required>
          <option>Carrots</option>
          <option>Peas</option>
          <option>Beans</option>
          <option>Pneumonoultramicroscopicsilicovolcanoconiosis</option>
        </select>
      </fieldset>
      <fieldset id="custom">
        <legend>Custom controls</legend>
        <select name="2A" id="select" autocomplete="off" required>
          <option>Carrots</option>
          <option>Peas</option>
          <option>Beans</option>
          <option>Pneumonoultramicroscopicsilicovolcanoconiosis</option>
        </select>
      </fieldset>
    </form>
    
    4 elements to create separate groups of options inside the dropdown.

    For further examples, see .

    This element includes the global attributes.

    A string providing a hint for a user agent's autocomplete feature. See The HTML autocomplete attribute for a complete list of values and details on how to use autocomplete.

    This Boolean attribute lets you specify that a form control should have input focus when the page loads. Only one form element in a document can have the

    <form>
      <fieldset>
        <legend>Standard controls</legend>
        <select name="1A" id="select" autocomplete="off" required>
          <option>Carrots</option>
          <option>Peas</option>
          <option>Beans</option>
          <option>Pneumonoultramicroscopicsilicovolcanoconiosis</option>
        </select>
      </fieldset>
      <fieldset id="custom">
        <legend>Custom controls</legend>
        <select name="2A" id="select" autocomplete="off" required>
          <option>Carrots</option>
          <option>Peas</option>
          <option>Beans</option>
          <option>Pneumonoultramicroscopicsilicovolcanoconiosis</option>
        </select>
      </fieldset>
    </form>
    
    2 attribute.

    This Boolean attribute indicates that the user cannot interact with the control. If this attribute is not specified, the control inherits its setting from the containing element, for example

    <form>
      <fieldset>
        <legend>Standard controls</legend>
        <select name="1A" id="select" autocomplete="off" required>
          <option>Carrots</option>
          <option>Peas</option>
          <option>Beans</option>
          <option>Pneumonoultramicroscopicsilicovolcanoconiosis</option>
        </select>
      </fieldset>
      <fieldset id="custom">
        <legend>Custom controls</legend>
        <select name="2A" id="select" autocomplete="off" required>
          <option>Carrots</option>
          <option>Peas</option>
          <option>Beans</option>
          <option>Pneumonoultramicroscopicsilicovolcanoconiosis</option>
        </select>
      </fieldset>
    </form>
    
    9; if there is no containing element with the
    <form>
      <fieldset>
        <legend>Standard controls</legend>
        <select name="1A" id="select" autocomplete="off" required>
          <option>Carrots</option>
          <option>Peas</option>
          <option>Beans</option>
          <option>Pneumonoultramicroscopicsilicovolcanoconiosis</option>
        </select>
      </fieldset>
      <fieldset id="custom">
        <legend>Custom controls</legend>
        <select name="2A" id="select" autocomplete="off" required>
          <option>Carrots</option>
          <option>Peas</option>
          <option>Beans</option>
          <option>Pneumonoultramicroscopicsilicovolcanoconiosis</option>
        </select>
      </fieldset>
    </form>
    
    1 attribute set, then the control is enabled.

    The

    body {
      font-family: Cambria, Cochin, Georgia, Times, "Times New Roman", serif;
    }
    
    .select:focus {
      border-color: blue;
    }
    
    html body form fieldset#custom div.select[data-multiple] div.header {
      display: none;
    }
    
    html body form fieldset#custom div.select div.header {
      content: "↓";
      display: flex;
      flex: 1;
      align-items: center;
      padding: 0;
      position: relative;
      width: auto;
      box-sizing: border-box;
      border-width: 1px;
      border-style: inherit;
      border-color: inherit;
      border-radius: inherit;
    }
    
    html body form fieldset#custom div.select div.header::after {
      content: "↓";
      align-self: stretch;
      display: flex;
      align-content: center;
      justify-content: center;
      justify-items: center;
      align-items: center;
      padding: 0.5em;
    }
    
    html body form fieldset#custom div.select div.header:hover::after {
      background-color: blue;
    }
    
    .select .header select {
      appearance: none;
      font-family: inherit;
      font-size: inherit;
      padding: 0;
      border-width: 0;
      width: 100%;
      flex: 1;
      display: none;
    }
    
    .select .header select optgroup {
      display: none;
    }
    
    .select select div.option {
      display: none;
    }
    
    html body form fieldset#custom div.select {
      user-select: none;
      box-sizing: border-box;
      position: relative;
      border-radius: 4px;
      border-style: solid;
      border-width: 0;
      border-color: gray;
      width: auto;
      display: inline-block;
    }
    
    html body form fieldset#custom div.select:focus,
    html body form fieldset#custom div.select:hover {
      border-color: blue;
    }
    
    html body form fieldset#custom div.select[data-open] {
      border-bottom-left-radius: 0;
      border-bottom-right-radius: 0;
    }
    
    html body form fieldset#custom div.select[data-open] datalist {
      display: initial;
    }
    
    html body form fieldset#custom div.select datalist {
      appearance: none;
      position: absolute;
      border-style: solid;
      border-width: 1px;
      border-color: gray;
      left: 0;
      display: none;
      width: 100%;
      box-sizing: border-box;
      z-index: 2;
      border-bottom-left-radius: 4px;
      border-bottom-right-radius: 4px;
    }
    
    html body form fieldset#custom div.select datalist div.option {
      background-color: white;
      margin-bottom: 1px;
      cursor: pointer;
      padding: 0.5em;
      border-width: 0;
    }
    
    html body form fieldset#custom div.select datalist div.option:hover,
    html body form fieldset#custom div.select datalist div.option:focus,
    html body form fieldset#custom div.select datalist div.option:checked {
      background-color: blue;
      color: white;
    }
    
    html
      body
      form
      fieldset#custom
      div.select
      div.optgroup
      div.option[data-disabled] {
      color: gray;
    }
    
    html
      body
      form
      fieldset#custom
      div.select
      div.optgroup
      div.option[data-checked] {
      background-color: blue;
      color: white;
    }
    
    html body form fieldset#custom div.select div.optgroup div.label {
      font-weight: bold;
    }
    
    html body form fieldset#custom div.select div.optgroup div.option div.label {
      font-weight: normal;
      padding: 0.25em;
    }
    
    html body form fieldset#custom div.select div.header span {
      flex: 1;
      padding: 0.5em;
    }
    
    2 element to associate the <select> with (its form owner). The value of this attribute must be the of a
    body {
      font-family: Cambria, Cochin, Georgia, Times, "Times New Roman", serif;
    }
    
    .select:focus {
      border-color: blue;
    }
    
    html body form fieldset#custom div.select[data-multiple] div.header {
      display: none;
    }
    
    html body form fieldset#custom div.select div.header {
      content: "↓";
      display: flex;
      flex: 1;
      align-items: center;
      padding: 0;
      position: relative;
      width: auto;
      box-sizing: border-box;
      border-width: 1px;
      border-style: inherit;
      border-color: inherit;
      border-radius: inherit;
    }
    
    html body form fieldset#custom div.select div.header::after {
      content: "↓";
      align-self: stretch;
      display: flex;
      align-content: center;
      justify-content: center;
      justify-items: center;
      align-items: center;
      padding: 0.5em;
    }
    
    html body form fieldset#custom div.select div.header:hover::after {
      background-color: blue;
    }
    
    .select .header select {
      appearance: none;
      font-family: inherit;
      font-size: inherit;
      padding: 0;
      border-width: 0;
      width: 100%;
      flex: 1;
      display: none;
    }
    
    .select .header select optgroup {
      display: none;
    }
    
    .select select div.option {
      display: none;
    }
    
    html body form fieldset#custom div.select {
      user-select: none;
      box-sizing: border-box;
      position: relative;
      border-radius: 4px;
      border-style: solid;
      border-width: 0;
      border-color: gray;
      width: auto;
      display: inline-block;
    }
    
    html body form fieldset#custom div.select:focus,
    html body form fieldset#custom div.select:hover {
      border-color: blue;
    }
    
    html body form fieldset#custom div.select[data-open] {
      border-bottom-left-radius: 0;
      border-bottom-right-radius: 0;
    }
    
    html body form fieldset#custom div.select[data-open] datalist {
      display: initial;
    }
    
    html body form fieldset#custom div.select datalist {
      appearance: none;
      position: absolute;
      border-style: solid;
      border-width: 1px;
      border-color: gray;
      left: 0;
      display: none;
      width: 100%;
      box-sizing: border-box;
      z-index: 2;
      border-bottom-left-radius: 4px;
      border-bottom-right-radius: 4px;
    }
    
    html body form fieldset#custom div.select datalist div.option {
      background-color: white;
      margin-bottom: 1px;
      cursor: pointer;
      padding: 0.5em;
      border-width: 0;
    }
    
    html body form fieldset#custom div.select datalist div.option:hover,
    html body form fieldset#custom div.select datalist div.option:focus,
    html body form fieldset#custom div.select datalist div.option:checked {
      background-color: blue;
      color: white;
    }
    
    html
      body
      form
      fieldset#custom
      div.select
      div.optgroup
      div.option[data-disabled] {
      color: gray;
    }
    
    html
      body
      form
      fieldset#custom
      div.select
      div.optgroup
      div.option[data-checked] {
      background-color: blue;
      color: white;
    }
    
    html body form fieldset#custom div.select div.optgroup div.label {
      font-weight: bold;
    }
    
    html body form fieldset#custom div.select div.optgroup div.option div.label {
      font-weight: normal;
      padding: 0.25em;
    }
    
    html body form fieldset#custom div.select div.header span {
      flex: 1;
      padding: 0.5em;
    }
    
    2 in the same document. (If this attribute is not set, the <select> is associated with its ancestor
    body {
      font-family: Cambria, Cochin, Georgia, Times, "Times New Roman", serif;
    }
    
    .select:focus {
      border-color: blue;
    }
    
    html body form fieldset#custom div.select[data-multiple] div.header {
      display: none;
    }
    
    html body form fieldset#custom div.select div.header {
      content: "↓";
      display: flex;
      flex: 1;
      align-items: center;
      padding: 0;
      position: relative;
      width: auto;
      box-sizing: border-box;
      border-width: 1px;
      border-style: inherit;
      border-color: inherit;
      border-radius: inherit;
    }
    
    html body form fieldset#custom div.select div.header::after {
      content: "↓";
      align-self: stretch;
      display: flex;
      align-content: center;
      justify-content: center;
      justify-items: center;
      align-items: center;
      padding: 0.5em;
    }
    
    html body form fieldset#custom div.select div.header:hover::after {
      background-color: blue;
    }
    
    .select .header select {
      appearance: none;
      font-family: inherit;
      font-size: inherit;
      padding: 0;
      border-width: 0;
      width: 100%;
      flex: 1;
      display: none;
    }
    
    .select .header select optgroup {
      display: none;
    }
    
    .select select div.option {
      display: none;
    }
    
    html body form fieldset#custom div.select {
      user-select: none;
      box-sizing: border-box;
      position: relative;
      border-radius: 4px;
      border-style: solid;
      border-width: 0;
      border-color: gray;
      width: auto;
      display: inline-block;
    }
    
    html body form fieldset#custom div.select:focus,
    html body form fieldset#custom div.select:hover {
      border-color: blue;
    }
    
    html body form fieldset#custom div.select[data-open] {
      border-bottom-left-radius: 0;
      border-bottom-right-radius: 0;
    }
    
    html body form fieldset#custom div.select[data-open] datalist {
      display: initial;
    }
    
    html body form fieldset#custom div.select datalist {
      appearance: none;
      position: absolute;
      border-style: solid;
      border-width: 1px;
      border-color: gray;
      left: 0;
      display: none;
      width: 100%;
      box-sizing: border-box;
      z-index: 2;
      border-bottom-left-radius: 4px;
      border-bottom-right-radius: 4px;
    }
    
    html body form fieldset#custom div.select datalist div.option {
      background-color: white;
      margin-bottom: 1px;
      cursor: pointer;
      padding: 0.5em;
      border-width: 0;
    }
    
    html body form fieldset#custom div.select datalist div.option:hover,
    html body form fieldset#custom div.select datalist div.option:focus,
    html body form fieldset#custom div.select datalist div.option:checked {
      background-color: blue;
      color: white;
    }
    
    html
      body
      form
      fieldset#custom
      div.select
      div.optgroup
      div.option[data-disabled] {
      color: gray;
    }
    
    html
      body
      form
      fieldset#custom
      div.select
      div.optgroup
      div.option[data-checked] {
      background-color: blue;
      color: white;
    }
    
    html body form fieldset#custom div.select div.optgroup div.label {
      font-weight: bold;
    }
    
    html body form fieldset#custom div.select div.optgroup div.option div.label {
      font-weight: normal;
      padding: 0.25em;
    }
    
    html body form fieldset#custom div.select div.header span {
      flex: 1;
      padding: 0.5em;
    }
    
    2 element, if any.)

    This attribute lets you associate <select> elements to

    body {
      font-family: Cambria, Cochin, Georgia, Times, "Times New Roman", serif;
    }
    
    .select:focus {
      border-color: blue;
    }
    
    html body form fieldset#custom div.select[data-multiple] div.header {
      display: none;
    }
    
    html body form fieldset#custom div.select div.header {
      content: "↓";
      display: flex;
      flex: 1;
      align-items: center;
      padding: 0;
      position: relative;
      width: auto;
      box-sizing: border-box;
      border-width: 1px;
      border-style: inherit;
      border-color: inherit;
      border-radius: inherit;
    }
    
    html body form fieldset#custom div.select div.header::after {
      content: "↓";
      align-self: stretch;
      display: flex;
      align-content: center;
      justify-content: center;
      justify-items: center;
      align-items: center;
      padding: 0.5em;
    }
    
    html body form fieldset#custom div.select div.header:hover::after {
      background-color: blue;
    }
    
    .select .header select {
      appearance: none;
      font-family: inherit;
      font-size: inherit;
      padding: 0;
      border-width: 0;
      width: 100%;
      flex: 1;
      display: none;
    }
    
    .select .header select optgroup {
      display: none;
    }
    
    .select select div.option {
      display: none;
    }
    
    html body form fieldset#custom div.select {
      user-select: none;
      box-sizing: border-box;
      position: relative;
      border-radius: 4px;
      border-style: solid;
      border-width: 0;
      border-color: gray;
      width: auto;
      display: inline-block;
    }
    
    html body form fieldset#custom div.select:focus,
    html body form fieldset#custom div.select:hover {
      border-color: blue;
    }
    
    html body form fieldset#custom div.select[data-open] {
      border-bottom-left-radius: 0;
      border-bottom-right-radius: 0;
    }
    
    html body form fieldset#custom div.select[data-open] datalist {
      display: initial;
    }
    
    html body form fieldset#custom div.select datalist {
      appearance: none;
      position: absolute;
      border-style: solid;
      border-width: 1px;
      border-color: gray;
      left: 0;
      display: none;
      width: 100%;
      box-sizing: border-box;
      z-index: 2;
      border-bottom-left-radius: 4px;
      border-bottom-right-radius: 4px;
    }
    
    html body form fieldset#custom div.select datalist div.option {
      background-color: white;
      margin-bottom: 1px;
      cursor: pointer;
      padding: 0.5em;
      border-width: 0;
    }
    
    html body form fieldset#custom div.select datalist div.option:hover,
    html body form fieldset#custom div.select datalist div.option:focus,
    html body form fieldset#custom div.select datalist div.option:checked {
      background-color: blue;
      color: white;
    }
    
    html
      body
      form
      fieldset#custom
      div.select
      div.optgroup
      div.option[data-disabled] {
      color: gray;
    }
    
    html
      body
      form
      fieldset#custom
      div.select
      div.optgroup
      div.option[data-checked] {
      background-color: blue;
      color: white;
    }
    
    html body form fieldset#custom div.select div.optgroup div.label {
      font-weight: bold;
    }
    
    html body form fieldset#custom div.select div.optgroup div.option div.label {
      font-weight: normal;
      padding: 0.25em;
    }
    
    html body form fieldset#custom div.select div.header span {
      flex: 1;
      padding: 0.5em;
    }
    
    2s anywhere in the document, not just inside a
    body {
      font-family: Cambria, Cochin, Georgia, Times, "Times New Roman", serif;
    }
    
    .select:focus {
      border-color: blue;
    }
    
    html body form fieldset#custom div.select[data-multiple] div.header {
      display: none;
    }
    
    html body form fieldset#custom div.select div.header {
      content: "↓";
      display: flex;
      flex: 1;
      align-items: center;
      padding: 0;
      position: relative;
      width: auto;
      box-sizing: border-box;
      border-width: 1px;
      border-style: inherit;
      border-color: inherit;
      border-radius: inherit;
    }
    
    html body form fieldset#custom div.select div.header::after {
      content: "↓";
      align-self: stretch;
      display: flex;
      align-content: center;
      justify-content: center;
      justify-items: center;
      align-items: center;
      padding: 0.5em;
    }
    
    html body form fieldset#custom div.select div.header:hover::after {
      background-color: blue;
    }
    
    .select .header select {
      appearance: none;
      font-family: inherit;
      font-size: inherit;
      padding: 0;
      border-width: 0;
      width: 100%;
      flex: 1;
      display: none;
    }
    
    .select .header select optgroup {
      display: none;
    }
    
    .select select div.option {
      display: none;
    }
    
    html body form fieldset#custom div.select {
      user-select: none;
      box-sizing: border-box;
      position: relative;
      border-radius: 4px;
      border-style: solid;
      border-width: 0;
      border-color: gray;
      width: auto;
      display: inline-block;
    }
    
    html body form fieldset#custom div.select:focus,
    html body form fieldset#custom div.select:hover {
      border-color: blue;
    }
    
    html body form fieldset#custom div.select[data-open] {
      border-bottom-left-radius: 0;
      border-bottom-right-radius: 0;
    }
    
    html body form fieldset#custom div.select[data-open] datalist {
      display: initial;
    }
    
    html body form fieldset#custom div.select datalist {
      appearance: none;
      position: absolute;
      border-style: solid;
      border-width: 1px;
      border-color: gray;
      left: 0;
      display: none;
      width: 100%;
      box-sizing: border-box;
      z-index: 2;
      border-bottom-left-radius: 4px;
      border-bottom-right-radius: 4px;
    }
    
    html body form fieldset#custom div.select datalist div.option {
      background-color: white;
      margin-bottom: 1px;
      cursor: pointer;
      padding: 0.5em;
      border-width: 0;
    }
    
    html body form fieldset#custom div.select datalist div.option:hover,
    html body form fieldset#custom div.select datalist div.option:focus,
    html body form fieldset#custom div.select datalist div.option:checked {
      background-color: blue;
      color: white;
    }
    
    html
      body
      form
      fieldset#custom
      div.select
      div.optgroup
      div.option[data-disabled] {
      color: gray;
    }
    
    html
      body
      form
      fieldset#custom
      div.select
      div.optgroup
      div.option[data-checked] {
      background-color: blue;
      color: white;
    }
    
    html body form fieldset#custom div.select div.optgroup div.label {
      font-weight: bold;
    }
    
    html body form fieldset#custom div.select div.optgroup div.option div.label {
      font-weight: normal;
      padding: 0.25em;
    }
    
    html body form fieldset#custom div.select div.header span {
      flex: 1;
      padding: 0.5em;
    }
    
    2. It can also override an ancestor
    body {
      font-family: Cambria, Cochin, Georgia, Times, "Times New Roman", serif;
    }
    
    .select:focus {
      border-color: blue;
    }
    
    html body form fieldset#custom div.select[data-multiple] div.header {
      display: none;
    }
    
    html body form fieldset#custom div.select div.header {
      content: "↓";
      display: flex;
      flex: 1;
      align-items: center;
      padding: 0;
      position: relative;
      width: auto;
      box-sizing: border-box;
      border-width: 1px;
      border-style: inherit;
      border-color: inherit;
      border-radius: inherit;
    }
    
    html body form fieldset#custom div.select div.header::after {
      content: "↓";
      align-self: stretch;
      display: flex;
      align-content: center;
      justify-content: center;
      justify-items: center;
      align-items: center;
      padding: 0.5em;
    }
    
    html body form fieldset#custom div.select div.header:hover::after {
      background-color: blue;
    }
    
    .select .header select {
      appearance: none;
      font-family: inherit;
      font-size: inherit;
      padding: 0;
      border-width: 0;
      width: 100%;
      flex: 1;
      display: none;
    }
    
    .select .header select optgroup {
      display: none;
    }
    
    .select select div.option {
      display: none;
    }
    
    html body form fieldset#custom div.select {
      user-select: none;
      box-sizing: border-box;
      position: relative;
      border-radius: 4px;
      border-style: solid;
      border-width: 0;
      border-color: gray;
      width: auto;
      display: inline-block;
    }
    
    html body form fieldset#custom div.select:focus,
    html body form fieldset#custom div.select:hover {
      border-color: blue;
    }
    
    html body form fieldset#custom div.select[data-open] {
      border-bottom-left-radius: 0;
      border-bottom-right-radius: 0;
    }
    
    html body form fieldset#custom div.select[data-open] datalist {
      display: initial;
    }
    
    html body form fieldset#custom div.select datalist {
      appearance: none;
      position: absolute;
      border-style: solid;
      border-width: 1px;
      border-color: gray;
      left: 0;
      display: none;
      width: 100%;
      box-sizing: border-box;
      z-index: 2;
      border-bottom-left-radius: 4px;
      border-bottom-right-radius: 4px;
    }
    
    html body form fieldset#custom div.select datalist div.option {
      background-color: white;
      margin-bottom: 1px;
      cursor: pointer;
      padding: 0.5em;
      border-width: 0;
    }
    
    html body form fieldset#custom div.select datalist div.option:hover,
    html body form fieldset#custom div.select datalist div.option:focus,
    html body form fieldset#custom div.select datalist div.option:checked {
      background-color: blue;
      color: white;
    }
    
    html
      body
      form
      fieldset#custom
      div.select
      div.optgroup
      div.option[data-disabled] {
      color: gray;
    }
    
    html
      body
      form
      fieldset#custom
      div.select
      div.optgroup
      div.option[data-checked] {
      background-color: blue;
      color: white;
    }
    
    html body form fieldset#custom div.select div.optgroup div.label {
      font-weight: bold;
    }
    
    html body form fieldset#custom div.select div.optgroup div.option div.label {
      font-weight: normal;
      padding: 0.25em;
    }
    
    html body form fieldset#custom div.select div.header span {
      flex: 1;
      padding: 0.5em;
    }
    
    2 element.

    This Boolean attribute indicates that multiple options can be selected in the list. If it is not specified, then only one option can be selected at a time. When

    <label>Please choose one or more pets:
      <select name="pets" multiple size="4">
        <optgroup label="4-legged pets">
          <option value="dog">Dog</option>
          <option value="cat">Cat</option>
          <option value="hamster" disabled>Hamster</option>
        </optgroup>
        <optgroup label="Flying pets">
          <option value="parrot">Parrot</option>
          <option value="macaw">Macaw</option>
          <option value="albatross">Albatross</option>
        </optgroup>
      </select>
    </label>
    
    8 is specified, most browsers will show a scrolling list box instead of a single line dropdown.

    This attribute is used to specify the name of the control.

    A Boolean attribute indicating that an option with a non-empty string value must be selected.

    If the control is presented as a scrolling list box (e.g. when

    <label>Please choose one or more pets:
      <select name="pets" multiple size="4">
        <optgroup label="4-legged pets">
          <option value="dog">Dog</option>
          <option value="cat">Cat</option>
          <option value="hamster" disabled>Hamster</option>
        </optgroup>
        <optgroup label="Flying pets">
          <option value="parrot">Parrot</option>
          <option value="macaw">Macaw</option>
          <option value="albatross">Albatross</option>
        </optgroup>
      </select>
    </label>
    
    8 is specified), this attribute represents the number of rows in the list that should be visible at one time. Browsers are not required to present a select element as a scrolled list box. The default value is
    const selects = custom.querySelectorAll('select');
    for (const select of selects) {
      const div = document.createElement('div');
      const header = document.createElement('div');
      const datalist = document.createElement('datalist');
      const optgroups = select.querySelectorAll('optgroup');
      const span = document.createElement('span');
      const options = select.options;
      const parent = select.parentElement;
      const multiple = select.hasAttribute('multiple');
      function onclick(e) {
        const disabled = this.hasAttribute('data-disabled');
        select.value = this.dataset.value;
        span.innerText = this.dataset.label;
        if (disabled) return;
        if (multiple) {
          if (e.shiftKey) {
            const checked = this.hasAttribute("data-checked");
            if (checked) {
              this.removeAttribute("data-checked");
            } else {
              this.setAttribute("data-checked", "");
            }
          } else {
            const options = div.querySelectorAll('.option');
            for (let i = 0; i < options.length; i++) {
              const option = options[i];
              option.removeAttribute("data-checked");
            }
            this.setAttribute("data-checked", "");
          }
        }
      }
    
      function onkeyup(e) {
        e.preventDefault();
        e.stopPropagation();
        if (e.keyCode === 13) {
          this.click();
        }
      }
    
      div.classList.add('select');
      header.classList.add('header');
      div.tabIndex = 1;
      select.tabIndex = -1;
      span.innerText = select.label;
      header.appendChild(span);
    
      for (const attribute of select.attributes) {
        div.dataset[attribute.name] = attribute.value;
      }
      for (let i = 0; i < options.length; i++) {
        const option = document.createElement('div');
        const label = document.createElement('div');
        const o = options[i];
        for (const attribute of o.attributes) {
          option.dataset[attribute.name] = attribute.value;
        }
        option.classList.add('option');
        label.classList.add('label');
        label.innerText = o.label;
        option.dataset.value = o.value;
        option.dataset.label = o.label;
        option.onclick = onclick;
        option.onkeyup = onkeyup;
        option.tabIndex = i + 1;
        option.appendChild(label);
        datalist.appendChild(option);
      }
      div.appendChild(header);
      for (const o of optgroups) {
        const optgroup = document.createElement('div');
        const label = document.createElement('div');
        const options = o.querySelectorAll('option');
    
        Object.assign(optgroup, o);
        optgroup.classList.add('optgroup');
        label.classList.add('label');
        label.innerText = o.label;
        optgroup.appendChild(label);
        div.appendChild(optgroup);
        for (const o of options) {
          const option = document.createElement('div');
          const label = document.createElement('div');
    
          for (const attribute of o.attributes) {
            option.dataset[attribute.name] = attribute.value;
          }
          option.classList.add('option');
          label.classList.add('label');
          label.innerText = o.label;
          option.tabIndex = i + 1;
          option.dataset.value = o.value;
          option.dataset.label = o.label;
          option.onclick = onclick;
          option.onkeyup = onkeyup;
          option.tabIndex = i + 1;
          option.appendChild(label);
          optgroup.appendChild(option);
        }
      }
    
      div.onclick = (e) => {
        e.preventDefault();
      };
    
      parent.insertBefore(div, select);
      header.appendChild(select);
      div.appendChild(datalist);
      datalist.style.top = `${header.offsetTop + header.offsetHeight}px`;
    
      div.onclick = (e) => {
        if (!multiple) {
          const open = this.hasAttribute("data-open");
          e.stopPropagation();
          if (open) {
            div.removeAttribute("data-open");
          } else {
            div.setAttribute("data-open", "");
          }
        }
      };
    
      div.onkeyup = (event) => {
        event.preventDefault();
        if (event.keyCode === 13) {
          div.click();
        }
      };
    
      document.addEventListener('click', (e) => {
        if (div.hasAttribute("data-open")) {
          div.removeAttribute("data-open");
        }
      });
    
      const width = Math.max(...Array.from(options).map((e) => {
        span.innerText = e.label;
        return div.offsetWidth;
      }));
    
      console.log(width);
      div.style.width = `${width}px`;
    }
    document.forms[0].onsubmit = (e) => {
      const data = new FormData(this);
      e.preventDefault();
      submit.innerText = JSON.stringify([...data.entries()]);
    };
    
    8.

    Note: According to the HTML specification, the default value for size should be

    const selects = custom.querySelectorAll('select');
    for (const select of selects) {
      const div = document.createElement('div');
      const header = document.createElement('div');
      const datalist = document.createElement('datalist');
      const optgroups = select.querySelectorAll('optgroup');
      const span = document.createElement('span');
      const options = select.options;
      const parent = select.parentElement;
      const multiple = select.hasAttribute('multiple');
      function onclick(e) {
        const disabled = this.hasAttribute('data-disabled');
        select.value = this.dataset.value;
        span.innerText = this.dataset.label;
        if (disabled) return;
        if (multiple) {
          if (e.shiftKey) {
            const checked = this.hasAttribute("data-checked");
            if (checked) {
              this.removeAttribute("data-checked");
            } else {
              this.setAttribute("data-checked", "");
            }
          } else {
            const options = div.querySelectorAll('.option');
            for (let i = 0; i < options.length; i++) {
              const option = options[i];
              option.removeAttribute("data-checked");
            }
            this.setAttribute("data-checked", "");
          }
        }
      }
    
      function onkeyup(e) {
        e.preventDefault();
        e.stopPropagation();
        if (e.keyCode === 13) {
          this.click();
        }
      }
    
      div.classList.add('select');
      header.classList.add('header');
      div.tabIndex = 1;
      select.tabIndex = -1;
      span.innerText = select.label;
      header.appendChild(span);
    
      for (const attribute of select.attributes) {
        div.dataset[attribute.name] = attribute.value;
      }
      for (let i = 0; i < options.length; i++) {
        const option = document.createElement('div');
        const label = document.createElement('div');
        const o = options[i];
        for (const attribute of o.attributes) {
          option.dataset[attribute.name] = attribute.value;
        }
        option.classList.add('option');
        label.classList.add('label');
        label.innerText = o.label;
        option.dataset.value = o.value;
        option.dataset.label = o.label;
        option.onclick = onclick;
        option.onkeyup = onkeyup;
        option.tabIndex = i + 1;
        option.appendChild(label);
        datalist.appendChild(option);
      }
      div.appendChild(header);
      for (const o of optgroups) {
        const optgroup = document.createElement('div');
        const label = document.createElement('div');
        const options = o.querySelectorAll('option');
    
        Object.assign(optgroup, o);
        optgroup.classList.add('optgroup');
        label.classList.add('label');
        label.innerText = o.label;
        optgroup.appendChild(label);
        div.appendChild(optgroup);
        for (const o of options) {
          const option = document.createElement('div');
          const label = document.createElement('div');
    
          for (const attribute of o.attributes) {
            option.dataset[attribute.name] = attribute.value;
          }
          option.classList.add('option');
          label.classList.add('label');
          label.innerText = o.label;
          option.tabIndex = i + 1;
          option.dataset.value = o.value;
          option.dataset.label = o.label;
          option.onclick = onclick;
          option.onkeyup = onkeyup;
          option.tabIndex = i + 1;
          option.appendChild(label);
          optgroup.appendChild(option);
        }
      }
    
      div.onclick = (e) => {
        e.preventDefault();
      };
    
      parent.insertBefore(div, select);
      header.appendChild(select);
      div.appendChild(datalist);
      datalist.style.top = `${header.offsetTop + header.offsetHeight}px`;
    
      div.onclick = (e) => {
        if (!multiple) {
          const open = this.hasAttribute("data-open");
          e.stopPropagation();
          if (open) {
            div.removeAttribute("data-open");
          } else {
            div.setAttribute("data-open", "");
          }
        }
      };
    
      div.onkeyup = (event) => {
        event.preventDefault();
        if (event.keyCode === 13) {
          div.click();
        }
      };
    
      document.addEventListener('click', (e) => {
        if (div.hasAttribute("data-open")) {
          div.removeAttribute("data-open");
        }
      });
    
      const width = Math.max(...Array.from(options).map((e) => {
        span.innerText = e.label;
        return div.offsetWidth;
      }));
    
      console.log(width);
      div.style.width = `${width}px`;
    }
    document.forms[0].onsubmit = (e) => {
      const data = new FormData(this);
      e.preventDefault();
      submit.innerText = JSON.stringify([...data.entries()]);
    };
    
    9; however, in practice, this has been found to break some websites, and no other browser currently does that, so Mozilla has opted to continue to return
    const selects = custom.querySelectorAll('select');
    for (const select of selects) {
      const div = document.createElement('div');
      const header = document.createElement('div');
      const datalist = document.createElement('datalist');
      const optgroups = select.querySelectorAll('optgroup');
      const span = document.createElement('span');
      const options = select.options;
      const parent = select.parentElement;
      const multiple = select.hasAttribute('multiple');
      function onclick(e) {
        const disabled = this.hasAttribute('data-disabled');
        select.value = this.dataset.value;
        span.innerText = this.dataset.label;
        if (disabled) return;
        if (multiple) {
          if (e.shiftKey) {
            const checked = this.hasAttribute("data-checked");
            if (checked) {
              this.removeAttribute("data-checked");
            } else {
              this.setAttribute("data-checked", "");
            }
          } else {
            const options = div.querySelectorAll('.option');
            for (let i = 0; i < options.length; i++) {
              const option = options[i];
              option.removeAttribute("data-checked");
            }
            this.setAttribute("data-checked", "");
          }
        }
      }
    
      function onkeyup(e) {
        e.preventDefault();
        e.stopPropagation();
        if (e.keyCode === 13) {
          this.click();
        }
      }
    
      div.classList.add('select');
      header.classList.add('header');
      div.tabIndex = 1;
      select.tabIndex = -1;
      span.innerText = select.label;
      header.appendChild(span);
    
      for (const attribute of select.attributes) {
        div.dataset[attribute.name] = attribute.value;
      }
      for (let i = 0; i < options.length; i++) {
        const option = document.createElement('div');
        const label = document.createElement('div');
        const o = options[i];
        for (const attribute of o.attributes) {
          option.dataset[attribute.name] = attribute.value;
        }
        option.classList.add('option');
        label.classList.add('label');
        label.innerText = o.label;
        option.dataset.value = o.value;
        option.dataset.label = o.label;
        option.onclick = onclick;
        option.onkeyup = onkeyup;
        option.tabIndex = i + 1;
        option.appendChild(label);
        datalist.appendChild(option);
      }
      div.appendChild(header);
      for (const o of optgroups) {
        const optgroup = document.createElement('div');
        const label = document.createElement('div');
        const options = o.querySelectorAll('option');
    
        Object.assign(optgroup, o);
        optgroup.classList.add('optgroup');
        label.classList.add('label');
        label.innerText = o.label;
        optgroup.appendChild(label);
        div.appendChild(optgroup);
        for (const o of options) {
          const option = document.createElement('div');
          const label = document.createElement('div');
    
          for (const attribute of o.attributes) {
            option.dataset[attribute.name] = attribute.value;
          }
          option.classList.add('option');
          label.classList.add('label');
          label.innerText = o.label;
          option.tabIndex = i + 1;
          option.dataset.value = o.value;
          option.dataset.label = o.label;
          option.onclick = onclick;
          option.onkeyup = onkeyup;
          option.tabIndex = i + 1;
          option.appendChild(label);
          optgroup.appendChild(option);
        }
      }
    
      div.onclick = (e) => {
        e.preventDefault();
      };
    
      parent.insertBefore(div, select);
      header.appendChild(select);
      div.appendChild(datalist);
      datalist.style.top = `${header.offsetTop + header.offsetHeight}px`;
    
      div.onclick = (e) => {
        if (!multiple) {
          const open = this.hasAttribute("data-open");
          e.stopPropagation();
          if (open) {
            div.removeAttribute("data-open");
          } else {
            div.setAttribute("data-open", "");
          }
        }
      };
    
      div.onkeyup = (event) => {
        event.preventDefault();
        if (event.keyCode === 13) {
          div.click();
        }
      };
    
      document.addEventListener('click', (e) => {
        if (div.hasAttribute("data-open")) {
          div.removeAttribute("data-open");
        }
      });
    
      const width = Math.max(...Array.from(options).map((e) => {
        span.innerText = e.label;
        return div.offsetWidth;
      }));
    
      console.log(width);
      div.style.width = `${width}px`;
    }
    document.forms[0].onsubmit = (e) => {
      const data = new FormData(this);
      e.preventDefault();
      submit.innerText = JSON.stringify([...data.entries()]);
    };
    
    8 for the time being with Firefox.

    On a desktop computer, there are a number of ways to select multiple options in a <select> element with a

    <label>Please choose one or more pets:
      <select name="pets" multiple size="4">
        <optgroup label="4-legged pets">
          <option value="dog">Dog</option>
          <option value="cat">Cat</option>
          <option value="hamster" disabled>Hamster</option>
        </optgroup>
        <optgroup label="Flying pets">
          <option value="parrot">Parrot</option>
          <option value="macaw">Macaw</option>
          <option value="albatross">Albatross</option>
        </optgroup>
      </select>
    </label>
    
    8 attribute:

    Mouse users can hold the Ctrl, Command, or Shift keys (depending on what makes sense for your operating system) and then click multiple options to select/deselect them.

    Warning: The mechanism for selecting multiple non-contiguous items via the keyboard described below currently only seems to work in Firefox.

    On macOS, the Ctrl + Up and Ctrl + Down shortcuts conflict with the OS default shortcuts for Mission Control and Application windows, so you'll have to turn these off before it will work.

    Keyboard users can select multiple contiguous items by:

    • Focusing on the <select> element (e.g. using Tab ).
    • Selecting an item at the top or bottom of the range they want to select using the Up and Down cursor keys to go up and down the options.
    • Holding down the Shift key and then using the Up and Down cursor keys to increase or decrease the range of items selected.

    Keyboard users can select multiple non-contiguous items by:

    • Focusing on the <select> element (e.g. using Tab ).
    • Holding down the Ctrl key then using the Up and Down cursor keys to change the "focused" select option, i.e. the one that will be selected if you choose to do so. The "focused" select option is highlighted with a dotted outline, in the same way as a keyboard-focused link.
    • Pressing Space to select/deselect "focused" select options.

    The <select> element is notoriously difficult to style productively with CSS. You can affect certain aspects like any element — for example, manipulating the box model, the displayed font, etc., and you can use the <select>6 property to remove the default system <select>6.

    However, these properties don't produce a consistent result across browsers, and it is hard to do things like line different types of form element up with one another in a column. The <select> element's internal structure is complex, and hard to control. If you want to get full control, you should consider using a library with good facilities for styling form widgets, or try rolling your own dropdown menu using non-semantic elements, JavaScript, and WAI-ARIA to provide semantics.

    For more useful information on styling <select>, see:

    • Styling HTML forms
    • Advanced styling for HTML forms

    Also see the "Customizing select styles" example below for an example of you could attempt a simple <select> styling.

    The following example creates a very simple dropdown menu, the second option of which is selected by default.

    <!-- The second value will be selected initially -->
    <select name="choice">
      <option value="first">First Value</option>
      <option value="second" selected>Second Value</option>
      <option value="third">Third Value</option>
    </select>
    

    The follow example is more complex, showing off more features you can use on a <select> element:

    <label>Please choose one or more pets:
      <select name="pets" multiple size="4">
        <optgroup label="4-legged pets">
          <option value="dog">Dog</option>
          <option value="cat">Cat</option>
          <option value="hamster" disabled>Hamster</option>
        </optgroup>
        <optgroup label="Flying pets">
          <option value="parrot">Parrot</option>
          <option value="macaw">Macaw</option>
          <option value="albatross">Albatross</option>
        </optgroup>
      </select>
    </label>
    

    You'll see that:

    • Multiple options are selectable because we've included the
      <label>Please choose one or more pets:
        <select name="pets" multiple size="4">
          <optgroup label="4-legged pets">
            <option value="dog">Dog</option>
            <option value="cat">Cat</option>
            <option value="hamster" disabled>Hamster</option>
          </optgroup>
          <optgroup label="Flying pets">
            <option value="parrot">Parrot</option>
            <option value="macaw">Macaw</option>
            <option value="albatross">Albatross</option>
          </optgroup>
        </select>
      </label>
      
      8 attribute.
    • The
      <label>Please choose one or more pets:
        <select name="pets" multiple size="4">
          <optgroup label="4-legged pets">
            <option value="dog">Dog</option>
            <option value="cat">Cat</option>
            <option value="hamster" disabled>Hamster</option>
          </optgroup>
          <optgroup label="Flying pets">
            <option value="parrot">Parrot</option>
            <option value="macaw">Macaw</option>
            <option value="albatross">Albatross</option>
          </optgroup>
        </select>
      </label>
      
      9 attribute causes only 4 lines to display at a time; you can scroll to view all the options.
    • We've included
      <form>
        <fieldset>
          <legend>Standard controls</legend>
          <select name="1A" id="select" autocomplete="off" required>
            <option>Carrots</option>
            <option>Peas</option>
            <option>Beans</option>
            <option>Pneumonoultramicroscopicsilicovolcanoconiosis</option>
          </select>
        </fieldset>
        <fieldset id="custom">
          <legend>Custom controls</legend>
          <select name="2A" id="select" autocomplete="off" required>
            <option>Carrots</option>
            <option>Peas</option>
            <option>Beans</option>
            <option>Pneumonoultramicroscopicsilicovolcanoconiosis</option>
          </select>
        </fieldset>
      </form>
      
      4 elements to divide the options up into different groups. This is a purely visual grouping, its visualization generally consists of the group name being bolded, and the options being indented.
    • The "Hamster" option includes a
      <form>
        <fieldset>
          <legend>Standard controls</legend>
          <select name="1A" id="select" autocomplete="off" required>
            <option>Carrots</option>
            <option>Peas</option>
            <option>Beans</option>
            <option>Pneumonoultramicroscopicsilicovolcanoconiosis</option>
          </select>
        </fieldset>
        <fieldset id="custom">
          <legend>Custom controls</legend>
          <select name="2A" id="select" autocomplete="off" required>
            <option>Carrots</option>
            <option>Peas</option>
            <option>Beans</option>
            <option>Pneumonoultramicroscopicsilicovolcanoconiosis</option>
          </select>
        </fieldset>
      </form>
      
      1 attribute and therefore can't be selected at all.

    This example shows how you could use some CSS and JavaScript to provide extensive custom styling for a <select> box.

    This example basically:

    • Clones the <select>'s context (the
      <label>Please choose one or more pets:
        <select name="pets" multiple size="4">
          <optgroup label="4-legged pets">
            <option value="dog">Dog</option>
            <option value="cat">Cat</option>
            <option value="hamster" disabled>Hamster</option>
          </optgroup>
          <optgroup label="Flying pets">
            <option value="parrot">Parrot</option>
            <option value="macaw">Macaw</option>
            <option value="albatross">Albatross</option>
          </optgroup>
        </select>
      </label>
      
      0s) in a parent wrapper and reimplements the standard expected behavior using additional HTML elements and JavaScript. This includes basic tab behavior to provide keyboard accessibility.
    • Maps some standards native <select>9 to id0 of the new elements in order to manage state and CSS.

    Note: Not all native features are supported, it's a Proof of Concept. IT starts from standard HTML but the same results can be achieved starting from JSON data, custom HTML, or other solutions.

    HTML

    <form>
      <fieldset>
        <legend>Standard controls</legend>
        <select name="1A" id="select" autocomplete="off" required>
          <option>Carrots</option>
          <option>Peas</option>
          <option>Beans</option>
          <option>Pneumonoultramicroscopicsilicovolcanoconiosis</option>
        </select>
      </fieldset>
      <fieldset id="custom">
        <legend>Custom controls</legend>
        <select name="2A" id="select" autocomplete="off" required>
          <option>Carrots</option>
          <option>Peas</option>
          <option>Beans</option>
          <option>Pneumonoultramicroscopicsilicovolcanoconiosis</option>
        </select>
      </fieldset>
    </form>
    

    CSS

    body {
      font-family: Cambria, Cochin, Georgia, Times, "Times New Roman", serif;
    }
    
    .select:focus {
      border-color: blue;
    }
    
    html body form fieldset#custom div.select[data-multiple] div.header {
      display: none;
    }
    
    html body form fieldset#custom div.select div.header {
      content: "↓";
      display: flex;
      flex: 1;
      align-items: center;
      padding: 0;
      position: relative;
      width: auto;
      box-sizing: border-box;
      border-width: 1px;
      border-style: inherit;
      border-color: inherit;
      border-radius: inherit;
    }
    
    html body form fieldset#custom div.select div.header::after {
      content: "↓";
      align-self: stretch;
      display: flex;
      align-content: center;
      justify-content: center;
      justify-items: center;
      align-items: center;
      padding: 0.5em;
    }
    
    html body form fieldset#custom div.select div.header:hover::after {
      background-color: blue;
    }
    
    .select .header select {
      appearance: none;
      font-family: inherit;
      font-size: inherit;
      padding: 0;
      border-width: 0;
      width: 100%;
      flex: 1;
      display: none;
    }
    
    .select .header select optgroup {
      display: none;
    }
    
    .select select div.option {
      display: none;
    }
    
    html body form fieldset#custom div.select {
      user-select: none;
      box-sizing: border-box;
      position: relative;
      border-radius: 4px;
      border-style: solid;
      border-width: 0;
      border-color: gray;
      width: auto;
      display: inline-block;
    }
    
    html body form fieldset#custom div.select:focus,
    html body form fieldset#custom div.select:hover {
      border-color: blue;
    }
    
    html body form fieldset#custom div.select[data-open] {
      border-bottom-left-radius: 0;
      border-bottom-right-radius: 0;
    }
    
    html body form fieldset#custom div.select[data-open] datalist {
      display: initial;
    }
    
    html body form fieldset#custom div.select datalist {
      appearance: none;
      position: absolute;
      border-style: solid;
      border-width: 1px;
      border-color: gray;
      left: 0;
      display: none;
      width: 100%;
      box-sizing: border-box;
      z-index: 2;
      border-bottom-left-radius: 4px;
      border-bottom-right-radius: 4px;
    }
    
    html body form fieldset#custom div.select datalist div.option {
      background-color: white;
      margin-bottom: 1px;
      cursor: pointer;
      padding: 0.5em;
      border-width: 0;
    }
    
    html body form fieldset#custom div.select datalist div.option:hover,
    html body form fieldset#custom div.select datalist div.option:focus,
    html body form fieldset#custom div.select datalist div.option:checked {
      background-color: blue;
      color: white;
    }
    
    html
      body
      form
      fieldset#custom
      div.select
      div.optgroup
      div.option[data-disabled] {
      color: gray;
    }
    
    html
      body
      form
      fieldset#custom
      div.select
      div.optgroup
      div.option[data-checked] {
      background-color: blue;
      color: white;
    }
    
    html body form fieldset#custom div.select div.optgroup div.label {
      font-weight: bold;
    }
    
    html body form fieldset#custom div.select div.optgroup div.option div.label {
      font-weight: normal;
      padding: 0.25em;
    }
    
    html body form fieldset#custom div.select div.header span {
      flex: 1;
      padding: 0.5em;
    }
    

    JavaScript

    const selects = custom.querySelectorAll('select');
    for (const select of selects) {
      const div = document.createElement('div');
      const header = document.createElement('div');
      const datalist = document.createElement('datalist');
      const optgroups = select.querySelectorAll('optgroup');
      const span = document.createElement('span');
      const options = select.options;
      const parent = select.parentElement;
      const multiple = select.hasAttribute('multiple');
      function onclick(e) {
        const disabled = this.hasAttribute('data-disabled');
        select.value = this.dataset.value;
        span.innerText = this.dataset.label;
        if (disabled) return;
        if (multiple) {
          if (e.shiftKey) {
            const checked = this.hasAttribute("data-checked");
            if (checked) {
              this.removeAttribute("data-checked");
            } else {
              this.setAttribute("data-checked", "");
            }
          } else {
            const options = div.querySelectorAll('.option');
            for (let i = 0; i < options.length; i++) {
              const option = options[i];
              option.removeAttribute("data-checked");
            }
            this.setAttribute("data-checked", "");
          }
        }
      }
    
      function onkeyup(e) {
        e.preventDefault();
        e.stopPropagation();
        if (e.keyCode === 13) {
          this.click();
        }
      }
    
      div.classList.add('select');
      header.classList.add('header');
      div.tabIndex = 1;
      select.tabIndex = -1;
      span.innerText = select.label;
      header.appendChild(span);
    
      for (const attribute of select.attributes) {
        div.dataset[attribute.name] = attribute.value;
      }
      for (let i = 0; i < options.length; i++) {
        const option = document.createElement('div');
        const label = document.createElement('div');
        const o = options[i];
        for (const attribute of o.attributes) {
          option.dataset[attribute.name] = attribute.value;
        }
        option.classList.add('option');
        label.classList.add('label');
        label.innerText = o.label;
        option.dataset.value = o.value;
        option.dataset.label = o.label;
        option.onclick = onclick;
        option.onkeyup = onkeyup;
        option.tabIndex = i + 1;
        option.appendChild(label);
        datalist.appendChild(option);
      }
      div.appendChild(header);
      for (const o of optgroups) {
        const optgroup = document.createElement('div');
        const label = document.createElement('div');
        const options = o.querySelectorAll('option');
    
        Object.assign(optgroup, o);
        optgroup.classList.add('optgroup');
        label.classList.add('label');
        label.innerText = o.label;
        optgroup.appendChild(label);
        div.appendChild(optgroup);
        for (const o of options) {
          const option = document.createElement('div');
          const label = document.createElement('div');
    
          for (const attribute of o.attributes) {
            option.dataset[attribute.name] = attribute.value;
          }
          option.classList.add('option');
          label.classList.add('label');
          label.innerText = o.label;
          option.tabIndex = i + 1;
          option.dataset.value = o.value;
          option.dataset.label = o.label;
          option.onclick = onclick;
          option.onkeyup = onkeyup;
          option.tabIndex = i + 1;
          option.appendChild(label);
          optgroup.appendChild(option);
        }
      }
    
      div.onclick = (e) => {
        e.preventDefault();
      };
    
      parent.insertBefore(div, select);
      header.appendChild(select);
      div.appendChild(datalist);
      datalist.style.top = `${header.offsetTop + header.offsetHeight}px`;
    
      div.onclick = (e) => {
        if (!multiple) {
          const open = this.hasAttribute("data-open");
          e.stopPropagation();
          if (open) {
            div.removeAttribute("data-open");
          } else {
            div.setAttribute("data-open", "");
          }
        }
      };
    
      div.onkeyup = (event) => {
        event.preventDefault();
        if (event.keyCode === 13) {
          div.click();
        }
      };
    
      document.addEventListener('click', (e) => {
        if (div.hasAttribute("data-open")) {
          div.removeAttribute("data-open");
        }
      });
    
      const width = Math.max(...Array.from(options).map((e) => {
        span.innerText = e.label;
        return div.offsetWidth;
      }));
    
      console.log(width);
      div.style.width = `${width}px`;
    }
    document.forms[0].onsubmit = (e) => {
      const data = new FormData(this);
      e.preventDefault();
      submit.innerText = JSON.stringify([...data.entries()]);
    };
    

    Result

    Content categories, , , , , , and elementPermitted contentZero or more

    <label>Please choose one or more pets:
      <select name="pets" multiple size="4">
        <optgroup label="4-legged pets">
          <option value="dog">Dog</option>
          <option value="cat">Cat</option>
          <option value="hamster" disabled>Hamster</option>
        </optgroup>
        <optgroup label="Flying pets">
          <option value="parrot">Parrot</option>
          <option value="macaw">Macaw</option>
          <option value="albatross">Albatross</option>
        </optgroup>
      </select>
    </label>
    
    0 or
    <form>
      <fieldset>
        <legend>Standard controls</legend>
        <select name="1A" id="select" autocomplete="off" required>
          <option>Carrots</option>
          <option>Peas</option>
          <option>Beans</option>
          <option>Pneumonoultramicroscopicsilicovolcanoconiosis</option>
        </select>
      </fieldset>
      <fieldset id="custom">
        <legend>Custom controls</legend>
        <select name="2A" id="select" autocomplete="off" required>
          <option>Carrots</option>
          <option>Peas</option>
          <option>Beans</option>
          <option>Pneumonoultramicroscopicsilicovolcanoconiosis</option>
        </select>
      </fieldset>
    </form>
    
    4 elements.Tag omissionNone, both the starting and ending tag are mandatory.Permitted parentsAny element that accepts .Implicit ARIA roleid3 with no
    <label>Please choose one or more pets:
      <select name="pets" multiple size="4">
        <optgroup label="4-legged pets">
          <option value="dog">Dog</option>
          <option value="cat">Cat</option>
          <option value="hamster" disabled>Hamster</option>
        </optgroup>
        <optgroup label="Flying pets">
          <option value="parrot">Parrot</option>
          <option value="macaw">Macaw</option>
          <option value="albatross">Albatross</option>
        </optgroup>
      </select>
    </label>
    
    8 attribute and no
    <label>Please choose one or more pets:
      <select name="pets" multiple size="4">
        <optgroup label="4-legged pets">
          <option value="dog">Dog</option>
          <option value="cat">Cat</option>
          <option value="hamster" disabled>Hamster</option>
        </optgroup>
        <optgroup label="Flying pets">
          <option value="parrot">Parrot</option>
          <option value="macaw">Macaw</option>
          <option value="albatross">Albatross</option>
        </optgroup>
      </select>
    </label>
    
    9 attribute greater than 1, otherwise id6Permitted ARIA rolesid7 with no
    <label>Please choose one or more pets:
      <select name="pets" multiple size="4">
        <optgroup label="4-legged pets">
          <option value="dog">Dog</option>
          <option value="cat">Cat</option>
          <option value="hamster" disabled>Hamster</option>
        </optgroup>
        <optgroup label="Flying pets">
          <option value="parrot">Parrot</option>
          <option value="macaw">Macaw</option>
          <option value="albatross">Albatross</option>
        </optgroup>
      </select>
    </label>
    
    8 attribute and no
    <label>Please choose one or more pets:
      <select name="pets" multiple size="4">
        <optgroup label="4-legged pets">
          <option value="dog">Dog</option>
          <option value="cat">Cat</option>
          <option value="hamster" disabled>Hamster</option>
        </optgroup>
        <optgroup label="Flying pets">
          <option value="parrot">Parrot</option>
          <option value="macaw">Macaw</option>
          <option value="albatross">Albatross</option>
        </optgroup>
      </select>
    </label>
    
    9 attribute greater than 1, otherwise no <label>0 permittedDOM interface<label>1