gengns ES

An entry from the gengns log, written by Génesis García Morilla · · CSS3, HTML5, JavaScript

Editable Selection Box

There is not any specific HTML tag to let you edit and select, we just have input tags and select tags but we can combine them and even more we can define our own icon/button for the select box. The trick here is to launch an event to show up our options from our hidden selection box.

JavaScript

const $button = document.querySelector('button');
const $select = document.querySelector('select');
const $input = document.querySelector('input');

$button.addEventListener('click', function() {
  if (!$select.style.display) {
    $select.style.display = 'block';
    $select.size = $select.querySelectorAll('option').length;
  } else $select.style.display = '';
});

$select.addEventListener('change', function() {
  $input.value = this.value;
  $input.parentNode.classList.add('is-dirty');
  $select.style.display = '';
});

HTML

<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<div class="mdl-grid">
  <div class="mdl-textfield mdl-js-textfield">
    <input class="mdl-textfield__input" type="text" id="film">
    <label class="mdl-textfield__label" for="film">Film...</label>
      <select>
        <option>Fight Club</option>
        <option>Howl's Moving Castle</option>
        <option>Life Is Beautiful</option>
    </select>
  </div>
  <button class="mdl-button mdl-js-button mdl-button--icon">
    <i class="material-icons">list</i>
  </button>
</div>

CSS

select {
  border: 0;
  position: absolute;
  top: 50px;
  display: none;
}

option {
  font-size: 16px;
}
Open in JSFiddle

You can adjust the options style, however in mobile apps the select options will be shown by an internal widget of the system.

Updated: you can use an input list with its datalist (Autocomplete dropdown)

Tags: input, Material, MDL, select