how to refresh Select2 dropdown menu after ajax loading different content?

how to refresh Select2 dropdown menu after ajax loading different content?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

How to Refresh Select2 Dropdown After AJAX Loading

When integrating dependent dropdowns, such as selecting a country first and then loading states via AJAX, developers often encounter two distinct issues: the initial display showing "undefined" instead of a placeholder, and the state selection persisting even after switching countries. Both problems stem from how Select2 initializes and how its internal data structures are managed during dynamic content updates.

Resolving the Initial "Undefined" Display

The primary issue arises because Select2 attempts to initialize immediately upon $(document).ready(). At this stage, while your HTML select element contains a default option (<option value="" selected>), Select2 might not have fully processed the DOM or it is trying to read the current value before the AJAX call completes. More critically, if you are relying on Select2's automatic initialization from the <select> tag, it often strips out the first option and treats an empty string as undefined if no specific data object exists in its internal cache.

To prevent "undefined" from appearing, you must ensure that Select2 is initialized with a valid placeholder configuration explicitly set after the DOM is ready but before any AJAX interaction occurs, or rely on the HTML structure while ensuring the initialization logic doesn't conflict with the default selection.

If you are using Select2's AJAX plugin to load data asynchronously, the standard approach is to initialize the widget first and then trigger the AJAX call manually once a country is selected. This prevents Select2 from trying to render options that don't exist yet.

$(document).ready(function() {
    $('#state').select2({
        placeholder: "Choose a State...",
        allowClear: true,
        width: '350px'
    });
});

By explicitly setting placeholder, you ensure that when the dropdown is empty (or cleared), Select2 renders your custom text. The key here is avoiding reliance on the visual <option> for the initial placeholder if you intend to use AJAX, as Select2 manages its own internal representation of the "no selection" state. If you must keep the HTML option for fallback reasons, ensure allowClear is set so the user can reset the value without triggering an AJAX reload immediately.

Handling State Persistence Across Country Changes

The second problem—where selecting "Arizona" in the US states list remains visible when switching to Canada—is a classic state management oversight. Select2 retains the currently selected object in its internal model. When you change the country and fetch new states, if you do not explicitly clear the previous selection, Select2 assumes the old value is still valid and simply filters the new list against it. Since "Arizona" is likely not in the Canada list, it remains visually present but non-selectable, or worse, causes a mismatch between the UI state and the actual data source.

To fix this, you must programmatically clear the selection on the state dropdown whenever the country changes. This involves calling .val('') to empty the value and then triggering a change event manually if your application logic requires it. Additionally, clearing the selection ensures that the placeholder is displayed again until a new valid state from the new country's list is chosen.

$('#country').on('change', function() {
    var selectedCountry = $(this).val();
    
    // Clear the previous state selection immediately
    $('#state').val('').trigger('change');
    
    // Trigger the AJAX call to load states for the new country
    loadStatesForCountry(selectedCountry);
});

function loadStatesForCountry(country) {
    $.ajax({
        url: '/your-states-endpoint',
        data: { country: country },
        success: function(data) {
            $('#state').html(data).trigger('select2:render');
            // Or if using the AJAX plugin, simply ensure the container is refreshed
            // often requires re-initializing or using .val() with new options
        }
    });
}

However, a more robust pattern involves re-rendering the Select2 instance or clearing its internal data model before injecting new options. If you are using the AJAX plugin (select2-ajax), simply calling .empty() followed by triggering the render event is usually sufficient to reset the view without losing the widget's configuration.

Best Practices for Dependent Dropdowns

The most reliable architecture avoids relying on HTML options for dynamic content entirely. Instead, build the entire <select> element programmatically in JavaScript or ensure your server-side rendering clears the options completely before injecting the new list. This eliminates ambiguity about whether a default option exists in the DOM versus what Select2 thinks is selected.

If you are using Laravel, passing the state options as a JSON response from your controller and reconstructing the Select2 container on success is cleaner than appending HTML strings directly to the select tag. This approach allows Select2 to manage its internal data structure correctly, preventing issues like "undefined" placeholders or stale selections. Always trigger the change event on the select element after AJAX success if you need to update dependent fields in your model or form validation logic.

Our network