Vanilla JavaScript

Vanilla JavaScript Dropdown

Vanilla JavaScript replacement for <select> element that makes the styling easy and consistent.

How it works

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.

Demo

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.

Download

Available on GitHub and npm

Installation

  1. Include the script
    <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.
  2. Include the CSS (feel free to edit it or write your own)
    <link rel="stylesheet" href="path/to/vanilla-js-dropdown.css">
  3. Write your select
    <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>
    
  4. Initialize the select
    var select = new CustomSelect({
        elem: "color-select",  // id of the original select element
    });
    
    // Open the select
    select.open();

Options

Option Required Description
elem yes id of the original select element or a direct DOM element

Methods

Method Description
.open() Opens the select
.close() Closes the select
.toggle() Opens the select if closed and vice-versa

Licence

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

Fork me on GitHub