Multi-select Dropdown with Checkboxes using jQuery

Selecting multiple values from a dropdown is a common requirement in web applications. However, the default HTML multiple select element does not always provide the most convenient user experience, particularly when users need to search through a large number of options.

In this tutorial, we will create a Multi-select Dropdown with Checkboxes using jQuery, HTML, CSS, and JavaScript. This custom dropdown allows users to select multiple items, search available options, select items from different groups, and manage their selections through an interactive interface.

The component is developed as a standalone jQuery plugin without depending on Bootstrap or any third-party multi-select library. It also provides a JavaScript API to retrieve, update, and clear selected values, making it easy to integrate into existing web applications.

We will explore 12 practical examples demonstrating different ways to implement and customize the jQuery multi-select dropdown, including submitting selected values to a PHP script.

Features of the jQuery Multi-select Dropdown

The jQuery multi-select dropdown component provides a wide range of features to enhance the user experience when selecting multiple options. It is designed to be flexible, accessible, and easy to integrate into web applications. Here are the main features supported by this component:

  • Checkbox selection: Select multiple options from a custom dropdown list.
  • Search functionality: Find options by entering keywords.
  • Grouped options: Organize dropdown items under group headings.
  • Group selection: Select or deselect all selectable options within a group.
  • Selection limits: Configure minimum and maximum selection requirements.
  • Preselected options: Set default selections during initialization.
  • Disabled options: Prevent users from selecting specific items.
  • Select All / Deselect All: Manage multiple selections conveniently.
  • Dynamic options: Replace dropdown options using JavaScript.
  • Selected item tags: Display selected values as removable tags.
  • JavaScript API: Get, set, clear, and manage selected values programmatically.
  • Form integration: Use the component with HTML forms and PHP processing.
  • Accessibility: Support keyboard navigation and ARIA attributes.
  • Multiple instances: Initialize independent dropdowns on the same page.

In the following sections, we will explore each feature in detail with practical examples and code snippets to help you implement the jQuery multi-select dropdown list with checkboxes in your web applications.

Installation and Initialization

First, include the plugin stylesheet, jQuery library, and JavaScript file in your HTML document.

<link rel="stylesheet" href="css/jquery.multiselect.css">

<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script src="js/jquery.multiselect.js"></script>

Make sure jQuery is loaded before the plugin JavaScript file.

Next, add a standard HTML select element to your page.

<select id="multiSelect"></select>

Initialize the multi-select dropdown using the multiSelectDropdown() method.

$('#multiSelect').multiSelectDropdown({
    placeholder: 'Select options...',
    options: [
        { value: 'php', label: 'PHP' },
        { value: 'javascript', label: 'JavaScript' },
        { value: 'python', label: 'Python' }
    ]
});

The plugin uses the original select element as its source and creates the custom checkbox-based dropdown interface.

Different Use Cases of jQuery Multi-select Dropdown

Let’s explore different configurations and practical use cases of this component.

1. Basic Multi-select Dropdown:

The basic multi-select dropdown allows users to choose multiple items from a list using checkboxes.

This configuration is useful when you need a simple multiple-selection interface without additional grouping or selection restrictions.

$('#multiSelect').multiSelectDropdown({
    placeholder: 'Select skills...',
    options: [
        { value: 'php', label: 'PHP' },
        { value: 'javascript', label: 'JavaScript' },
        { value: 'python', label: 'Python' },
        { value: 'java', label: 'Java' },
        { value: 'csharp', label: 'C#' }
    ]
});

When users select an option, it appears as a removable tag inside the dropdown control. Multiple options can be selected without closing the dropdown.

2. Search Enabled:

When a dropdown contains many options, finding a particular item manually can be inconvenient. The built-in search functionality solves this problem by filtering options as users type.

Set search to true to enable the search field.

$('#multiSelect').multiSelectDropdown({
    placeholder: 'Search and select skills...',
    search: true,
    options: [
        { value: 'php', label: 'PHP' },
        { value: 'javascript', label: 'JavaScript' },
        { value: 'python', label: 'Python' },
        { value: 'java', label: 'Java' },
        { value: 'csharp', label: 'C#' },
        { value: 'react', label: 'React' }
    ]
});

The search is case-insensitive and filters matching options dynamically. Existing selections remain unchanged when the search results are filtered.

If no matching option is found, the dropdown displays a “No results found” message.

3. Grouped Options:

Grouping related options makes a multi-select dropdown easier to navigate, particularly when dealing with different categories of items.

You can define grouped options using the group and options properties.

$('#multiSelect').multiSelectDropdown({
    placeholder: 'Select technologies...',
    options: [
        {
            group: 'Frontend',
            options: [
                { value: 'html', label: 'HTML' },
                { value: 'css', label: 'CSS' },
                { value: 'javascript', label: 'JavaScript' },
                { value: 'react', label: 'React' }
            ]
        },
        {
            group: 'Backend',
            options: [
                { value: 'php', label: 'PHP' },
                { value: 'python', label: 'Python' },
                { value: 'nodejs', label: 'Node.js' }
            ]
        }
    ]
});

The dropdown displays separate headings for Frontend and Backend, with their respective options underneath.

This approach is useful for organizing skills, product categories, departments, services, and other related data.

4. Group Selection:

If a dropdown contains multiple groups, selecting individual options one by one may take time.

Enable group selection using the allowGroupSelection option.

$('#multiSelect').multiSelectDropdown({
    placeholder: 'Select technologies...',
    allowGroupSelection: true,
    options: [
        {
            group: 'Frontend',
            options: [
                { value: 'html', label: 'HTML' },
                { value: 'css', label: 'CSS' },
                { value: 'javascript', label: 'JavaScript' }
            ]
        },
        {
            group: 'Backend',
            options: [
                { value: 'php', label: 'PHP' },
                { value: 'python', label: 'Python' },
                { value: 'nodejs', label: 'Node.js' }
            ]
        }
    ]
});

Users can select or deselect all selectable options within a group by clicking its group checkbox.

The group checkbox automatically reflects the selection status:

  • Unchecked: None of the selectable options are selected.
  • Checked: All selectable options are selected.
  • Indeterminate: Only some selectable options are selected.

This feature is particularly useful when users need to select multiple items from specific categories.

5. Maximum Selection Limit:

Some forms require users to select only a limited number of options. For example, a registration form may allow users to choose up to three skills.

You can enforce this restriction using maxSelection.

$('#multiSelect').multiSelectDropdown({
    placeholder: 'Select up to 3 skills...',
    maxSelection: 3,
    disableWhenMaxReached: true,
    options: [
        { value: 'php', label: 'PHP' },
        { value: 'javascript', label: 'JavaScript' },
        { value: 'python', label: 'Python' },
        { value: 'java', label: 'Java' },
        { value: 'csharp', label: 'C#' }
    ]
});

In this example, users can select a maximum of three options. Once the limit is reached, the remaining unselected options are disabled automatically.

You can also configure a minimum selection requirement using minSelection.

$('#multiSelect').multiSelectDropdown({
    minSelection: 1,
    maxSelection: 3
});

This configuration requires at least one selection and allows a maximum of three selected options.

6. Preselected Options:

You can specify which options should be selected when the dropdown is initialized.

Set the selected property to true for the required options.

$('#multiSelect').multiSelectDropdown({
    placeholder: 'Select skills...',
    options: [
        { value: 'php', label: 'PHP', selected: true },
        { value: 'javascript', label: 'JavaScript', selected: true },
        { value: 'python', label: 'Python' },
        { value: 'java', label: 'Java' }
    ]
});

In this example, PHP and JavaScript are selected by default.

Preselected options are useful when editing existing records or displaying previously saved user preferences.

7. Disabled Options:

You may need to display certain options without allowing users to select them.

Use the disabled property to disable individual options.

$('#multiSelect').multiSelectDropdown({
    placeholder: 'Select skills...',
    options: [
        { value: 'php', label: 'PHP' },
        { value: 'javascript', label: 'JavaScript' },
        { value: 'python', label: 'Python', disabled: true },
        { value: 'java', label: 'Java', disabled: true }
    ]
});

Disabled options remain visible in the dropdown but cannot be selected through mouse or keyboard interaction.

8. Select / Deselect All:

When users need to select many options, selecting each checkbox individually is inconvenient.

The plugin provides Select All and Deselect All actions to simplify this process.

$('#multiSelect').multiSelectDropdown({
    placeholder: 'Select skills...',
    selectAll: true,
    clearButton: true,
    options: [
        { value: 'php', label: 'PHP' },
        { value: 'javascript', label: 'JavaScript' },
        { value: 'python', label: 'Python' },
        { value: 'java', label: 'Java' }
    ]
});

The Select All action selects available options, while Deselect All clears the selections.

These actions respect disabled options and the configured selection limits. When search is enabled, Select All also supports selecting the currently filtered results.

9. Dynamic Options:

You can replace the available dropdown options dynamically without destroying and recreating the component.

The updateOptions method accepts a new array of flat or grouped options.

// Initial dropdown options
$('#multiSelect').multiSelectDropdown({
    placeholder: 'Select technologies...',
    options: [
        { value: 'php', label: 'PHP' },
        { value: 'python', label: 'Python' }
    ]
});

// New dropdown options
var newOptions = [
    { value: 'html', label: 'HTML' },
    { value: 'css', label: 'CSS' },
    { value: 'javascript', label: 'JavaScript' },
    { value: 'react', label: 'React' }
];

// Update the dropdown
$('#multiSelect').multiSelectDropdown(
    'updateOptions',
    newOptions
);

The existing options are replaced with the new options. Previously selected values are preserved if they are still available in the updated list.

This method is useful when the available options change based on user actions or other application requirements.

10. JavaScript API (Set / Get / Clear):

The plugin provides public JavaScript methods to manage selected values programmatically.

These methods are particularly useful when integrating the dropdown with other form controls or application logic.

Get selected values
Use getValue to retrieve an array containing the selected option values.

var selectedValues = $('#multiSelect')
    .multiSelectDropdown('getValue');

console.log(selectedValues);

Example output:

['php', 'javascript']

Get selected option objects
Use getSelectedOptions to retrieve the selected option objects, including their associated properties.

var selectedOptions = $('#multiSelect')
    .multiSelectDropdown('getSelectedOptions');

console.log(selectedOptions);

Set selected values
Use setValue to programmatically select specific options.

$('#multiSelect').multiSelectDropdown(
    'setValue',
    ['php', 'javascript']
);

Clear selected values
Use the clear method to clear the current selections, subject to the configured minimum selection limit.

$('#multiSelect').multiSelectDropdown('clear');

Destroy the dropdown
If you no longer need the custom dropdown, use destroy to remove the plugin interface and restore the original select element.

$('#multiSelect').multiSelectDropdown('destroy');

Define Options in the HTML Select Element

Besides providing options through JavaScript, you can define them directly inside the original HTML select element.

The plugin supports standard <option> and <optgroup> elements.

<select id="multiSelect" multiple>
    <option value="php">PHP</option>
    <option value="javascript" selected>JavaScript</option>
    <option value="python">Python</option>

    <optgroup label="Frontend">
        <option value="html">HTML</option>
        <option value="css">CSS</option>
        <option value="react">React</option>
    </optgroup>

    <optgroup label="Backend">
        <option value="laravel">Laravel</option>
        <option value="nodejs">Node.js</option>
    </optgroup>
</select>

Initialize the plugin without providing an options array.

$('#multiSelect').multiSelectDropdown({
    placeholder: 'Select technologies...',
    search: true
});

The plugin reads the available options, selected values, and groups from the original select element.

This approach is convenient when options are generated dynamically by PHP or another server-side language.

Submit Selected Values to a PHP Script

The jQuery multi-select dropdown can also be integrated into a PHP form to process multiple selected values on the server.

For this example, we will create a simple skills selection form and submit the selected values to a PHP script.

Step 1: Create the HTML form
Add a name` attribute ending with [] to the multiple select element. This allows PHP to receive the submitted values as an array.

<form action="submit.php" method="POST">

    <label for="multiSelect">Select Skills:</label>

    <select id="multiSelect" name="skills[]" multiple>
        <option value="php">PHP</option>
        <option value="javascript">JavaScript</option>
        <option value="python">Python</option>
        <option value="react">React</option>
        <option value="laravel">Laravel</option>
    </select>

    <button type="submit">Submit</button>

</form>

Step 2: Initialize the multi-select dropdown
Use jQuery to initialize the custom dropdown.

$('#multiSelect').multiSelectDropdown({
    placeholder: 'Select your skills...',
    search: true,
    selectAll: true,
    maxSelection: 3
});

Step 3: Process the submitted values using PHP
Create a submit.php file to receive and process the selected values.

<?php 
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    
// Get selected options from the form submission
    
$selectedOptions = isset($_POST['skills']) ? $_POST['skills'] : [];

    if (!empty(
$selectedOptions)) {
        echo 
"Selected skills: " . implode(", ", $selectedOptions);
    } else {
        echo 
'No skills selected.';
    }
}
?>

When the form is submitted, PHP processes the selected values from the skills array and displays the valid selections.

Note: Client-side selection limits should not be treated as server-side validation. Always validate submitted values against the allowed options on the server.

Also, the plugin must synchronize selections with the original HTML select element for native form submission. Ensure this behavior is available in the plugin implementation before using this example.

Handle Selection Changes with a Callback

The plugin provides an onChange callback that runs whenever the selected options change.

You can use this callback to update other page elements, perform calculations, or execute application-specific logic.

$('#multiSelect').multiSelectDropdown({
    options: [
        { value: 'php', label: 'PHP' },
        { value: 'javascript', label: 'JavaScript' },
        { value: 'python', label: 'Python' }
    ],
    onChange: function (selectedValues, selectedOptions) {
        console.log('Selected Values:', selectedValues);
        console.log('Selected Options:', selectedOptions);
    }
});

The callback receives two arguments:

  • selectedValues: An array of selected option values.
  • selectedOptions: An array of selected option objects.

Listen for the jQuery Change Event

In addition to the callback, the plugin emits a jQuery event named change.multiselectDropdown on the original select element.

You can listen for this event to handle selection changes separately.

$('#multiSelect').on(
    'change.multiselectDropdown',
    function (event, values, options) {
        console.log('Selected Values:', values);
        console.log('Selected Options:', options);
    }
);

Configuration Options

The following table lists the available configuration options and their default values.

| Option | Default | Description |
|---|---|---|
| `placeholder` | `Select options...` | Placeholder text |
| `options` | `[]` | Flat or grouped options |
| `search` | `true` | Enable search |
| `selectAll` | `true` | Show Select All and Deselect All |
| `clearButton` | `true` | Show Clear button |
| `maxSelection` | `null` | Maximum selected values |
| `minSelection` | `0` | Minimum selected values |
| `maxDisplayed` | `3` | Maximum displayed tags |
| `closeOnSelect` | `false` | Close dropdown after selection |
| `showGroupHeadings` | `true` | Group heading display option |
| `allowGroupSelection` | `true` | Enable group selection |
| `width` | `null` | Custom dropdown width |
| `maxHeight` | `260` | Maximum options height |
| `disableWhenMaxReached` | `true` | Disable unselected options at maximum |
| `onChange` | `null` | Selection change callback |

Keyboard Navigation and Accessibility

The component also supports keyboard navigation and accessibility features, making it easier to use without a mouse.

The supported keyboard interactions include:

  • Arrow Down / Arrow Up: Open the dropdown and navigate through available options.
  • Enter / Space: Toggle the focused option or group control.
  • Escape: Close the dropdown and return focus to the main control.
  • Tab: Navigate through standard interactive controls.

The plugin uses ARIA attributes and combobox, listbox, option, and expanded-state semantics to improve accessibility.

Disabled options are excluded from keyboard selection.

Conclusion

In this tutorial, we have demonstrated how to implement a reusable jQuery multi-select dropdown with checkboxes and customize it for different application requirements.

From basic multiple selection and searchable options to grouped items, selection limits, dynamic option management, and PHP form submission, the component provides a flexible alternative to the standard HTML multiple select element.

Since it does not require Bootstrap or any third-party multi-select library, you can easily integrate it into existing HTML, PHP, and other web application projects.

Download the source code, explore the examples, and customize the dropdown to suit your project requirements.

Looking for expert assistance to implement or extend this script’s functionality? Submit a Service Request

21 Comments

  1. Vinutha B Said...
  2. Rodel Barbin Said...
  3. Ramesh Said...
  4. Vishal Said...
  5. Tariq Khan Said...
  6. John Said...
  7. Sohail Said...
  8. Bharath Said...
    • CodexWorld Said...
  9. Saurabh Said...
  10. Vinay Jain Said...
    • CodexWorld Said...
  11. Deepak Said...
  12. Kiruba Said...
  13. Naseer Said...
  14. Chakra Pani Said...
  15. Thom Said...
  16. Leo Said...
  17. Kharliah Said...
  18. Parthiban Said...
  19. Ankit Sahu Said...

Leave a reply

construction Need this implemented in your project? Request Implementation Help → keyboard_double_arrow_up