Select2 makes HTML select menus easier to search and use, but clearing a selection can be confusing if you are not sure whether to change the original field, call a Select2 method, or reset the whole form. In most cases, the simplest solution is to set the underlying select element’s value to null or an empty array and trigger a jQuery change event. This guide explains how to clear single and multiple Select2 dropdowns, add a clear button, handle AJAX-backed fields, and avoid common mistakes.
What Does “Clear a Select2 Dropdown” Mean?
“Clear” can refer to more than one action. You might want to remove the selected value while keeping the dropdown available, close the open menu, or erase text typed into the search box. These are different tasks, and each needs a different solution.
- Clear the selection: Remove the currently selected option or options.
- Close the dropdown: Hide the options menu without changing the selected value.
- Clear the search term: Remove text entered in Select2’s search field.
- Reset the form: Restore the field to its original default value.
This article focuses primarily on clearing the selection, which is what most developers mean when they ask how to clear a Select2 dropdown in jQuery.
Clear a Single-Select Select2 Dropdown
For a single-select field, set the underlying select element’s value to null and trigger change. Select2 listens for this event and updates what it displays.
$('#mySelect').val(null).trigger('change');
Replace #mySelect with the ID or selector for your select element. For example, this button clears the selected value:
<select id="mySelect" style="width: 100%;">
<option value=""></option>
<option value="red">Red</option>
<option value="blue">Blue</option>
</select>
<button type="button" id="clearSelect">Clear selection</button>
<script>
$('#mySelect').select2({
placeholder: 'Choose a color'
});
$('#clearSelect').on('click', function () {
$('#mySelect').val(null).trigger('change');
});
</script>
The call to trigger('change') matters. Changing the value through jQuery does not, by itself, tell Select2 to refresh its displayed selection. Triggering the event keeps the original select element, the Select2 interface, and other change-event handlers in sync.
Clear a Multiple-Select Select2 Dropdown
A multiple-select field can contain several selected values, so clear it by assigning an empty array and triggering change:
$('#myMultiSelect').val([]).trigger('change');
You can also use null in many Select2 setups, but an empty array clearly communicates that the multiple-select field should have no selected options. A complete example looks like this:
<select id="myMultiSelect" multiple="multiple" style="width: 100%;">
<option value="html">HTML</option>
<option value="css">CSS</option>
<option value="jquery">jQuery</option>
</select>
<button type="button" id="clearMultiple">Clear all</button>
<script>
$('#myMultiSelect').select2({
placeholder: 'Choose skills'
});
$('#clearMultiple').on('click', function () {
$('#myMultiSelect').val([]).trigger('change');
});
</script>
After the button is clicked, all selected tags are removed, and the placeholder appears if one is configured.
Show a Clear Button with allowClear
Select2 can display a built-in clear control for a single-select field. Enable it with the allowClear option and provide a placeholder:
<select id="country" style="width: 100%;">
<option value=""></option>
<option value="ca">Canada</option>
<option value="gb">United Kingdom</option>
<option value="us">United States</option>
</select>
<script>
$('#country').select2({
placeholder: 'Select a country',
allowClear: true
});
</script>
For a single-select dropdown, include a blank first option. Select2 uses the placeholder to represent the empty state, and the blank option allows the field to return to that state when cleared. The clear control generally appears when an option is selected; its exact appearance can vary by Select2 version and theme.
allowClear is not a substitute for clearing the value in your own code. Use .val(null).trigger('change') when a button, event, or other application logic needs to clear the field programmatically.
Clear a Select2 Field After a Form Action
You may want to clear a field after a user submits a form, saves a record, or completes another action. Put the clearing call inside the relevant event handler:
$('#saveButton').on('click', function () {
// Save the form data here.
$('#category').val(null).trigger('change');
$('#tags').val([]).trigger('change');
});
Use null for a single-select field and an empty array for a multiple-select field. If the field is inside a form, you can also reset the form to its default values, but that behaves differently from clearing every field. A form reset restores the values specified when the page loaded; it does not necessarily leave each Select2 field empty.
Clear an AJAX-Powered Select2 Dropdown
Select2 fields that load options through AJAX can also be cleared with the standard jQuery value-and-change pattern. For a single-select AJAX field, use:
$('#remoteUser').val(null).trigger('change');
For a multiple-select AJAX field, use:
$('#remoteUsers').val([]).trigger('change');
It is usually not necessary to remove options manually just to clear the selection. The selected item’s value is stored on the underlying select element, and changing that value with a change event updates the Select2 display. Avoid deleting options from the DOM unless you specifically intend to remove those options from the list as well.
If you add a new option programmatically, Select2 may need an actual option element before its value can be selected. That is a separate task from clearing the field. For a straightforward clear operation, first check that you are targeting the correct select element and triggering the event.
Run Code When Select2 Is Cleared
If your application needs to respond when the user clears a field, listen for Select2’s select2:clear event:
$('#country').on('select2:clear', function () {
console.log('The country selection was cleared.');
});
You can also listen for the normal jQuery change event, which fires when the selection changes:
$('#country').on('change', function () {
if (!$(this).val()) {
console.log('No country is selected.');
}
});
The select2:clear event is useful when you specifically need to detect Select2’s clear action. The change event is useful for general value changes, including changes made elsewhere in your code. If you clear a field programmatically with .val(null).trigger('change'), your change handlers will run. Do not assume that every programmatic change represents a user clicking the clear control.
Close the Dropdown Without Clearing the Selection
If you only want to hide the open options menu, call Select2’s close method:
$('#mySelect').select2('close');
This closes the dropdown but leaves the selected value unchanged. To clear the value and then close the menu, you can combine the operations:
$('#mySelect').val(null).trigger('change');
$('#mySelect').select2('close');
Keeping these actions separate helps prevent an unexpected result. For example, closing a menu is appropriate after a selection is made, while clearing a value is appropriate when the user wants to remove their choice.
Clear the Select2 Search Box
The search field inside Select2 is separate from the selected value. If the user has typed a search term, clearing the selection does not always mean that you have also cleared that text. In most cases, closing and reopening the dropdown resets the search interaction naturally. You can close the Select2 control with:
$('#mySelect').select2('close');
Avoid depending on internal Select2 CSS classes to manipulate the search input directly. Those details can differ between Select2 versions, themes, and whether the search field is inside the dropdown or above it. If your requirement is to clear the selected item, use the select element’s value instead of trying to edit the search box.
Common Mistakes When Clearing Select2
-
Forgetting to trigger change: Calling
.val(null)alone may change the underlying value without refreshing the Select2 interface. Add.trigger('change'). -
Using the wrong value for a multi-select: Use an empty array, such as
.val([]), to remove all selected items from a multiple-select field. -
Omitting the blank option: A single-select field using a placeholder and
allowClearshould include an empty first option. -
Clearing the wrong element: Apply
.val()to the original<select>, not to a generated Select2 container. -
Confusing close with clear:
.select2('close')hides the menu; it does not remove the selected value. - Removing options unnecessarily: Clearing a selection does not require deleting the available options from the select element.
Also verify that Select2 has been initialized before calling Select2-specific methods such as .select2('close'). The standard jQuery value-and-change approach works on the original select element, but the Select2 display can only update correctly when the plugin is initialized and listening to that element.
Quick Reference
-
Clear one selected value:
$('#mySelect').val(null).trigger('change'); -
Clear all values in a multi-select:
$('#myMultiSelect').val([]).trigger('change'); -
Enable a user-facing clear control: Configure
allowClear: trueand provide a placeholder. -
Close the menu only:
$('#mySelect').select2('close'); -
Listen for Select2’s clear event: Bind a handler to
select2:clear.
Conclusion
To clear a Select2 dropdown in jQuery, update the value of the original select element and trigger a change event. Use $('#mySelect').val(null).trigger('change') for a single-select field and $('#myMultiSelect').val([]).trigger('change') for a multiple-select field. Add allowClear: true with a placeholder when users need a built-in clear control. If your goal is only to hide the menu, use .select2('close') instead. Choosing the right action keeps the Select2 display and your application’s form data in sync.
Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.