npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

multi-selectbox-js

v1.2.0

Published

A lightweight, vanilla JavaScript library for creating interactive multi-select dropdowns with tag-based UI

Downloads

40

Readme

Multi-SelectBox JS

A lightweight, vanilla JavaScript library for creating interactive multi-select dropdowns with tag-based UI.

🎮 View Live Demo

Credits

This library is inspired by and built upon the excellent work of Habib Mhamadi's multi-select-tag repository. We've enhanced the original implementation with additional features and customizations:

Original Features (from multi-select-tag):

  • Multi-select functionality with search and filter
  • Tag-based UI with remove functionality
  • Keyboard navigation support
  • Automatic synchronization with hidden select elements

Additional Features & Enhancements:

  • Color Customization: Full control over colors for all UI elements
  • Tag Display Management: Interactive "+ X more" indicator with dropdown
  • Class-based Architecture: Refactored to modern JavaScript class structure
  • Auto CSS Injection: No manual CSS imports required
  • NPM Package: Published as a reusable npm package
  • Enhanced Documentation: Comprehensive examples and API documentation
  • Instance-Specific Styling: Each dropdown can have unique color schemes

Thank you to Habib Mhamadi for the original inspiration and foundation!

Features

  • Search & Filter: Filter options dynamically as you type
  • Keyboard Navigation: Use arrow keys to navigate and Enter to select
  • Tag Display Control: Limit visible tags with interactive "+ X more" indicator
  • Automatic Sync: All changes sync with the hidden <select> element
  • Public API: Helper methods selectAll(), clearAll(), and getSelectedTags()
  • Multiple Instances: Each instance is independent and encapsulated with unique IDs
  • No Dependencies: Pure vanilla JavaScript with no external dependencies
  • Auto CSS Injection: CSS is automatically loaded - no manual CSS import needed
  • Form-Ready: Multiple select boxes can be used in a single form without conflicts

Installation

NPM

npm install multi-selectbox-js

CDN

<script src="https://unpkg.com/[email protected]/dist/multi-selectbox-js.js"></script>

Direct File Usage (after npm install)

If you've installed the package via npm, you can also use it directly in HTML files:

<script src="node_modules/multi-selectbox-js/dist/multi-selectbox-js.js"></script>

Quick Start

Method 1: NPM Installation

npm install multi-selectbox-js
<!DOCTYPE html>
<html>
<head>
    <!-- CSS is automatically injected by the JS file -->
    <script src="node_modules/multi-selectbox-js/dist/multi-selectbox-js.js"></script>
</head>
<body>
    <select id="countries" multiple>
        <option value="us">United States</option>
        <option value="uk">United Kingdom</option>
        <option value="ca">Canada</option>
        <option value="au">Australia</option>
    </select>

    <script>
        const multiSelect = new MultiSelectBox('countries', {
            placeholder: 'Select countries...',
            maxDisplayTags: 2
        });
    </script>
</body>
</html>

Method 2: CDN Usage

<!DOCTYPE html>
<html>
<head>
    <!-- CSS is automatically injected by the JS file -->
    <script src="https://unpkg.com/[email protected]/dist/multi-selectbox-js.js"></script>
</head>
<body>
    <select id="countries" multiple>
        <option value="us">United States</option>
        <option value="uk">United Kingdom</option>
        <option value="ca">Canada</option>
        <option value="au">Australia</option>
    </select>

    <script>
        const multiSelect = new MultiSelectBox('countries', {
            placeholder: 'Select countries...',
            maxDisplayTags: 2
        });
    </script>
</body>
</html>

Method 3: Module Import

// ES6 Modules
import MultiSelectBox from 'multi-selectbox-js';

// CommonJS
const MultiSelectBox = require('multi-selectbox-js');

const multiSelect = new MultiSelectBox('countries', {
    placeholder: 'Select countries...',
    maxDisplayTags: 2
});

Configuration Options

| Option | Type | Default | Description | |--------|------|---------|-------------| | placeholder | string | 'Search' | Placeholder text for the input field | | maxSelection | number | Infinity | Maximum number of items that can be selected | | maxDisplayTags | number | Infinity | Maximum number of visible tags (shows "+ X more" for overflow) | | required | boolean | false | Whether the field is required | | onChange | function | null | Callback function when selection changes | | addAllOption | boolean | false | Enable/disable the "All" option functionality | | allOptionText | string | 'Select All' | Custom text for the "All" option display |

Examples

Basic Usage

const multiSelect = new MultiSelectBox('mySelect', {
    placeholder: 'Choose options...'
});

With Display Limit

const multiSelect = new MultiSelectBox('mySelect', {
    maxDisplayTags: 1,  // Show only 1 tag + "+ X more"
    placeholder: 'Select items...'
});

With Selection Limit

const multiSelect = new MultiSelectBox('mySelect', {
    maxSelection: 5,    // Allow only 5 selections
    maxDisplayTags: 3,  // Show 3 tags + "+ X more"
    onChange: (tags) => console.log('Selected:', tags)
});

With "All" Option

const multiSelect = new MultiSelectBox('mySelect', {
    addAllOption: true,           // Enable "All" option
    allOptionText: 'Select All',  // Custom text for "All" option
    maxDisplayTags: 3,
    onChange: (tags) => console.log('Selected:', tags)
});

Public API

Methods

selectAll()

Selects all available options (respects maxSelection limit).

multiSelect.selectAll();

clearAll()

Clears all selections.

multiSelect.clearAll();

getSelectedTags()

Returns an array of currently selected tags.

const selected = multiSelect.getSelectedTags();
console.log(selected); // [{id: 'us', label: 'United States'}, ...]

"All" Option Functionality

When addAllOption: true is enabled, the MultiSelectBox provides intelligent "All" option management:

Features:

  • Auto-placement: "All" option always appears first in the dropdown and selections
  • Smart selection: Selecting "All" automatically selects all other options
  • Smart deselection: Deselecting "All" clears all selections
  • Auto-management: Selecting all individual options automatically adds "All"
  • Mutual exclusivity: Selecting individual options when "All" is selected removes "All"
  • Custom text: Use allOptionText to customize the display text

Behavior Examples:

// When "All" is selected
// All other options are automatically selected
// "All" appears first in the selected tags list

// When individual options are selected
// If all individual options become selected, "All" is automatically added

// When "All" is deselected
// All other selections are cleared

// When individual options are deselected from "All"
// "All" is automatically removed, other selections remain

Multiple Instances in Forms

You can use multiple MultiSelectBox instances in a single form without any conflicts. Each instance gets unique IDs automatically:

<form>
    <select id="countries" multiple>
        <option value="us">United States</option>
        <option value="uk">United Kingdom</option>
        <option value="ca">Canada</option>
    </select>
    
    <select id="languages" multiple>
        <option value="js">JavaScript</option>
        <option value="py">Python</option>
        <option value="java">Java</option>
    </select>
    
    <select id="colors" multiple>
        <option value="red">Red</option>
        <option value="blue">Blue</option>
        <option value="green">Green</option>
    </select>
</form>

<script>
    // Each instance works independently with unique IDs
    const countriesSelect = new MultiSelectBox('countries', {
        maxSelection: 3,
        placeholder: 'Select countries...'
    });
    
    const languagesSelect = new MultiSelectBox('languages', {
        maxDisplayTags: 1,
        placeholder: 'Select languages...'
    });
    
    const colorsSelect = new MultiSelectBox('colors', {
        colors: {
            tagBackground: '#ff6b6b',
            tagText: '#ffffff'
        },
        placeholder: 'Select colors...'
    });
</script>

Key Benefits:

  • No ID Conflicts: Each instance gets unique internal IDs
  • Independent Functionality: Each dropdown works completely independently
  • Individual Styling: Each instance can have its own color scheme
  • Form Submission: All selected values are properly synced to the original <select> elements

Interactive Features

Tag Display Management

  • Clickable "+ X more" indicator: Click to view all selected tags
  • Individual tag removal: Click the × button next to any tag to remove it
  • Automatic dropdown hiding: The dropdown closes when all tags are removed
  • Consistent styling: Follows the same design patterns as the main dropdown

Keyboard Navigation

  • Arrow Up/Down: Navigate through dropdown options
  • Enter: Select highlighted option
  • Backspace: Remove last selected tag (when input is empty)

Color Customization

You can customize the colors of various elements using the colors configuration option:

const multiSelect = new MultiSelectBox('mySelect', {
    colors: {
        // Container colors
        containerBorder: '#e5e7eb',
        containerBackground: '#ffffff',
        
        // Tag colors
        tagBackground: '#DBEAFE',
        tagText: '#1E40AF',
        tagBorder: '#3B82F6',
        
        // Dropdown colors
        dropdownBorder: '#e5e7eb',
        dropdownBackground: '#ffffff',
        dropdownItemBackground: '#E5E7EB',
        dropdownItemText: '#374151',
        
        // More indicator colors
        moreIndicatorBackground: '#F3F4F6',
        moreIndicatorText: '#6B7280',
        
        // Selected tag dropdown colors
        selectedTagDropdownBackground: '#ffffff',
        selectedTagDropdownText: '#1E40AF',
        selectedTagDropdownHover: '#f9fafb'
    }
});

Available Color Options

| Color Option | Description | Default | |--------------|-------------|---------| | containerBorder | Border color of the main container | #e5e7eb | | containerBackground | Background color of the main container | #ffffff | | tagBackground | Background color of selected tags | #DBEAFE | | tagText | Text color of selected tags | #1E40AF | | tagBorder | Border color of selected tags | #3B82F6 | | dropdownBorder | Border color of the dropdown | #e5e7eb | | dropdownBackground | Background color of the dropdown | #ffffff | | dropdownItemBackground | Background color of dropdown items on hover | #E5E7EB | | dropdownItemText | Text color of dropdown items | #374151 | | moreIndicatorBackground | Background color of "+ X more" indicator | #F3F4F6 | | moreIndicatorText | Text color of "+ X more" indicator | #6B7280 | | selectedTagDropdownBackground | Background color of selected tags dropdown | #ffffff | | selectedTagDropdownText | Text color of selected tags in dropdown | #1E40AF | | selectedTagDropdownHover | Background color of selected tags on hover in dropdown | #f9fafb |

Color Customization Examples

Dark Theme:

const multiSelect = new MultiSelectBox('mySelect', {
    colors: {
        containerBorder: '#374151',
        containerBackground: '#1F2937',
        tagBackground: '#3B82F6',
        tagText: '#ffffff',
        dropdownBackground: '#1F2937',
        dropdownItemBackground: '#374151',
        dropdownItemText: '#ffffff'
    }
});

Custom Brand Colors:

const multiSelect = new MultiSelectBox('mySelect', {
    colors: {
        tagBackground: '#FF6B6B',
        tagText: '#ffffff',
        moreIndicatorBackground: '#4ECDC4',
        moreIndicatorText: '#ffffff'
    }
});

CSS Handling

The library automatically injects the required CSS when loaded. You don't need to manually import any CSS files.

Automatic CSS Injection

  • NPM Package: CSS is automatically loaded from CDN
  • CDN Usage: CSS is automatically loaded from the same CDN
  • Direct File Usage: CSS is automatically loaded from CDN

Manual CSS Import (Optional)

If you prefer to import CSS manually, you can do so:

// Import CSS manually (optional)
import 'multi-selectbox-js/dist/multi-selectbox-js.css';
<!-- Manual CSS import (optional) -->
<link rel="stylesheet" href="node_modules/multi-selectbox-js/dist/multi-selectbox-js.css">

Browser Support

  • Chrome 60+
  • Firefox 55+
  • Safari 12+
  • Edge 79+

License

MIT License - see LICENSE file for details.

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Changelog

v1.0.0

  • Initial release
  • Multi-select functionality with search
  • Tag display management with "+ X more" indicator
  • Keyboard navigation support
  • Public API methods