Reads the original <select> element's options (with respect of «selected», if any), creates and attaches the pseudo-select just before the original one, and hides the original.
Upon selection it updates the original <select>, so when you submit your form the value will be there.
JavaScript disabled? No problem! Nicely degrades to original <select>.
— Inspired by the blazing fast, lightweight, cross-platform Vanilla JS framework.
Click Submit to see the value of the original select changing.
Feel free to look at the source (Ctrl + U
) or open the console (F12
) to make sure the submitted data retrieved from the original select element.
<script src="path/to/vanilla-js-dropdown.min.js"></script>
or install it from npm
npm i vanilla-js-dropdown
A simple usage example can be found in this code playground.
<link rel="stylesheet" href="path/to/vanilla-js-dropdown.css">
<select id="color-select" name="color">
<optgroup label="Red colors">
<option value="indianRed">IndianRed</option>
<option value="salmon">Salmon</option>
<option value="crimson">Crimson</option>
</optgroup>
<optgroup label="Green colors">
<option value="chartreuse">Chartreuse</option>
<option value="seagreen">SeaGreen</option>
<option value="olive">Olive</option>
</optgroup>
<optgroup label="Blue colors">
<option value="aqua" disabled>Aqua</option>
<option value="steelblue">SteelBlue</option>
<option value="royalblue">RoyalBlue</option>
</optgroup>
</select>
var select = new CustomSelect({
elem: "color-select", // id of the original select element
});
// Open the select
select.open();
Option | Required | Description |
---|---|---|
elem | yes | id of the original select element or a direct DOM element |
Method | Description |
---|---|
.open() | Opens the select |
.close() | Closes the select |
.toggle() | Opens the select if closed and vice-versa |
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to http://unlicense.org