Dropdown
Converts a select menu into a menu made of divs, so that you can style it.
- Every
<select>on the page is converted, except those with the classsystem. - The
<select>stays in the page, hidden off screen and marked with the classjs-styled, so forms still submit it. The custom select is added right after it. - When a different option is chosen, a real
changeevent is fired on the<select>(see below). optgroupand multiple selection are not supported.- It is not accessible yet: there is no keyboard support.
<form id="dropdown-form" action="#"> <div class="dropdown-row"> <label for="dropdown-month">Month you were born: </label> <select id="dropdown-month" name="dropdown-month"> <option value="1">January</option> <option value="2">February</option> <option value="3">March</option> <option value="4">April</option> <option value="5">May</option> <option value="6">June</option> <option value="7">July</option> <option value="8">August</option> <option value="9">September</option> <option value="10">October</option> <option value="11">November</option> <option value="12">December</option> </select> <span id="dropdown-output">Nothing chosen yet.</span> </div>
<div class="dropdown-row"> <label for="dropdown-fruit">Favorite fruit: </label> <select id="dropdown-fruit" name="dropdown-fruit"> <option value="apple">Apple</option> <option value="orange" selected>Orange</option> <option value="kiwi" disabled>Kiwi (out of season)</option> <option value="mango">Mango</option> </select> </div>
<div class="dropdown-row"> <label for="dropdown-system">A select with the class "system": </label> <select id="dropdown-system" class="system" name="dropdown-system"> <option>Left alone</option> <option>Still a normal select</option> </select> </div>
<div class="dropdown-row" id="dropdown-container"> <button type="button" id="dropdown-convert">add a select</button> </div></form>var month = document.getElementById('dropdown-month');var output = document.getElementById('dropdown-output');var container = document.getElementById('dropdown-container');var added = 0;
// Choosing an option fires a real change event on the <select>, so an ordinary listener hears it.month.addEventListener('change', function () { output.textContent = 'month ' + month.value;});
// The script build converts the page when the DOM is ready. For <select>s that are added later,// call ML.Dropdown(): it converts the ones that are not converted yet and leaves the rest alone.document.getElementById('dropdown-convert').addEventListener('click', function () { added++;
var select = document.createElement('select'); select.name = 'dropdown-added-' + added; select.innerHTML = '<option>Created with JavaScript #' + added + '</option><option>Another option</option>'; container.appendChild(select);
ML.Dropdown();});Converting the page
Section titled “Converting the page”- With a
<script>tag, the page is converted when the DOM is ready.ML.Dropdownis a function: callML.Dropdown()to convert the<select>s you added since. The ones that are already converted are left alone, so it is safe to call as often as you like. The demo’s “add a select” button does this. - With modules, create one
Dropdownand callinit(). Callinit()again after adding<select>s, in the same way.destroy()removes the custom selects and every listener, and gives the<select>s back.
The markup it creates
Section titled “The markup it creates”<div class="dropdown" data-value="apple" style="width: 114px;"> <a href="#" class="dropdown-link" tabindex="-1" data-index="0">Apple</a> <ul class="dropdown-menu"> <li data-value="apple" data-index="0"><a href="#" tabindex="-1">Apple</a></li> <li data-value="orange" data-index="1"><a href="#" tabindex="-1">Orange</a></li> <li class="disabled" data-value="kiwi" data-index="2"><a href="#" tabindex="-1">Kiwi</a></li> </ul></div>The library’s CSS only hides the <select> and positions the menu. The colors, borders and the arrow are up to
your page. The classes focus and active are set on .dropdown while it has focus and while it is open, and
selected on the <li> of the current option.
Events
Section titled “Events”Choosing a different option fires a real change event on the <select>, after its value has changed. It bubbles, so
select.addEventListener('change', ...) and a listener on the form both hear it, with an event as the argument.
Choosing the option that is already chosen fires nothing.
select.addEventListener('change', function (event) { console.log('chosen', event.target.value);});The focus and blur of the <select> are the browser’s own: they fire when it is reached with the keyboard.
Opening the menu fires no event on it.
- The width of the custom select is measured once, when it is created, from the width of its menu. Create it
when the
<select>is visible and your styles are loaded: a<select>that is hidden at that moment gets a width of1px. - It removes the whitespace-only text nodes from the parent of the
<select>, so that no gaps appear between inline elements. - A disabled
<select>or option can not be chosen. A disabled option does not close the menu when clicked. - It converts every
<select>on the page, so give the ones you want to keep the classsystem. - The class names are generic:
.dropdown,.dropdown-linkand.dropdown-menu. Do not load its CSS next to a framework that uses the same names (Bootstrap’s dropdown does): the rules would reach that framework’s markup.
See the generated Dropdown class reference for every method.
Enhancements
Section titled “Enhancements”A checklist. An unticked item is not built, and not promised: it says what is missing today. A ticked item has been built.
- Keyboard. Arrow keys, Home and End, Enter, Esc, and typing to find an option.
- Screen readers.
role="listbox"andaria-expanded, so the custom select is more than a list of links. -
optgroupandmultiple. Neither is supported. - Options added later. The menu is made once, so options added to the
<select>afterwards are not in it. Arefresh()would read them again. - The width. It is measured once, when the custom select is made, so it is wrong if the
<select>was hidden or the styles were not loaded yet. - Opening upward. The menu always opens downward and is shortened to fit; it could open upward when there is more room above.
- The native picker on a touch screen. Showing the browser’s own picker there would be easier to use than a custom menu.