Skip to content

Repository files navigation

Multi Select Dropdown JS

npm version License: MIT

Create powerful user interfaces with our Multi Select Dropdown! This tool enhances native select elements, allowing for multiple selections, dynamic content generation, integrated search functionality, and customizable UI without any dependencies. No jQuery or other library is required!

Complete Guide & Reference: https://codeshack.io/multi-select-dropdown-html-javascript/
Interactive Demo: https://codeshack.io/multi-select-dropdown-js/

Features

  • Multiple Selections: Users can select more than one option in the dropdown.
  • OptGroups & Sublists: Natively parses HTML <optgroup> tags and automatically generates master toggle switches for groups.
  • Native Form Validation: Fully supports the HTML5 required attribute with perfectly positioned browser tooltips.
  • Dark Mode & Themes: Built-in support for auto (follows OS preference), light, and dark themes.
  • Async Data Fetching: Easily load external JSON arrays from APIs using the built-in .fetch() method.
  • Search Functionality: Includes a built-in search to find options quickly.
  • Dynamic & Reactive: Update options dynamically via JS setters. The UI instantly reacts to data changes.
  • Secure & Accessible: Built-in XSS protection, memory-leak proof, framework-agnostic, and fully keyboard navigable.
  • Lightweight: Lightweight in size and does not depend on other libraries.

Screenshot

Screenshot of Multi Select Dropdown

Installation

You can integrate the library into your project using NPM, a CDN, or by downloading the files manually.

Option 1: NPM (Recommended)

Install the package via npm:

npm install multi-select-dropdown-js

Then, import the CSS and JS into your project:

import 'multi-select-dropdown-js/MultiSelect.min.css';
import 'multi-select-dropdown-js/MultiSelect.min.js';

Selects with the data-multi-select attribute are converted automatically. To create instances in your own code, import the class instead:

import 'multi-select-dropdown-js/MultiSelect.min.css';
import MultiSelect from 'multi-select-dropdown-js';

new MultiSelect('#example-multi-select', { placeholder: 'Select options' });

Option 2: CDN (jsDelivr)

For rapid prototyping or classic HTML workflows, include the minified files directly via jsDelivr.

Add the CSS to your <head> tag:

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/multi-select-dropdown-js/MultiSelect.min.css">

Add the JavaScript in your <head> or just before your closing </body> tag:

<script src="https://cdn.jsdelivr.net/npm/multi-select-dropdown-js/MultiSelect.min.js"></script>

Option 3: Manual Download

Clone the repository or download the ZIP, and include the files in your project:

<link rel="stylesheet" href="path/to/MultiSelect.min.css">
<script src="path/to/MultiSelect.min.js"></script>

Usage

Here's a simple example to add the multi-select dropdown to your project.

(Tip: Always append [] to your name attribute if you are submitting data to a backend like PHP!)

Each option value should be unique within a list, because the selection and the submitted form data are matched by value.

Method A: Pure HTML Data Attributes

<select id="example-multi-select" name="options[]" data-placeholder="Select options" multiple data-multi-select>
    <option value="option1">Option 1</option>
    <option value="option2">Option 2</option>
    <!-- more options -->
</select>

Method B: JavaScript Initialization

<select id="example-multi-select" name="options[]" multiple>
    <option value="option1">Option 1</option>
    <option value="option2">Option 2</option>
    <!-- more options -->
</select>

<script>
    new MultiSelect('#example-multi-select', {
        placeholder: 'Select options',
        theme: 'auto'
    });
</script>

Configuration

You can pass a settings object to customize the dropdown behavior and styling:

new MultiSelect('#example-multi-select', {
    placeholder: 'Select options',
    theme: 'auto', // 'auto', 'light', or 'dark'
    min: 1,  // Minimum number of items required
    max: 5,  // Maximum number of items that can be selected
    search: true,  // Enable the search box
    selectAll: true,  // Add a "Select All" toggle
    listAll: true, // If false, shows "X selected" instead of listing tags
    closeListOnItemSelect: false, // Auto-close the dropdown after selection
    required: true, // Enforce native HTML5 form validation
    
    onChange: function(value, text, element) {
        console.log('Change:', value, text, element);
    },
    onSelect: function(value, text, element) {
        console.log('Selected:', value, text, element);
    },
    onUnselect: function(value, text, element) {
        console.log('Unselected:', value, text, element);
    },
    onMaxReached: function(max) {
        console.log('Maximum selections reached:', max);
    }
});

Dynamically Adding Options & Groups

You can dynamically populate the dropdown with grouped data, disabled options, and safe custom HTML injection:

const mySelect = new MultiSelect('#example-multi-select', {
    data:[
        {
            value: 'opt1',
            text: 'Option 1',
            group: 'Basic Settings'
        },
        {
            value: 'opt2',
            html: '<strong style="color: red;">Option 2 with HTML!</strong>',
            group: 'Basic Settings'
        },
        {
            value: 'opt3',
            text: 'Option 3',
            selected: true,
            group: 'Advanced Settings'
        },
        {
            value: 'opt4',
            text: 'Locked Option',
            disabled: true // Prevents user interaction
        }
    ],
    placeholder: 'Select options',
    search: true,
    selectAll: true
});

Asynchronous Data Fetching

Loading data from a remote endpoint is handled natively:

const asyncSelect = new MultiSelect('#example-multi-select', {
    placeholder: 'Loading remote data...'
});

// Fetches a JSON array formatted like the data object above
asyncSelect.fetch('https://api.yoursite.com/endpoint');

Changelog

1.0.4

  • Selected items in the header now appear in the same order as the list. They were shown in reverse.
  • The script now works when it's loaded in the <head> without defer. Selects with data-multi-select are converted once the page has loaded, and loading it before </body> works as before.
  • "Select all" and group headers now work on a filtered list. The first click selects the items that match the search, the second click unselects them, and items outside the search keep their state. Their checkmarks follow the matching items.
  • Calling destroy() before the page has finished loading is no longer undone by the automatic setup, and one element that fails to convert no longer stops the rest.
  • The file can now be imported with a bundler or require()d (module.exports), and loading it where there is no document no longer throws.

License

Distributed under the MIT License. See LICENSE for more information.

Contact

David Adams - info@codeshack.io

GitHub: https://github.com/codeshackio/multi-select-dropdown-js

X (Twitter): https://twitter.com/codeshackio

Feel free to open an issue or submit pull requests.

Releases

Sponsor this project

Packages

Contributors

Languages