🧩 Fix a Native Multi-Select Dropdown Silently Losing Every Selected Option When Its Option List Is Rebuilt From JavaScript
Rebuilding a native select’s options from JavaScript – clearing it out and appending a fresh set after an API response or a filter change – is a completely normal pattern for a single-select dropdown, but on a select with the multiple attribute it has a side effect that’s easy to miss: every option element you remove takes its selected state with it, and replacing those elements with brand-new ones (even if they have identical values and text) creates options that start life unselected, because selection is a property of the specific option node, not of its value matching something the user picked before.
🔎 The Problem
const select = document.querySelector('select[multiple]');
const previouslySelected = /* user had picked values: 2, 5, 9 */;
// Rebuilding the option list after new data arrives:
select.innerHTML = ''; // destroys the old <option> nodes...
newData.forEach(item => {
const opt = document.createElement('option');
opt.value = item.id;
opt.textContent = item.label;
select.appendChild(opt); // ...and these NEW nodes are
// unselected by default, even for
// ids 2, 5, and 9 that the user
// had previously picked
});
// Result: the user's selection is silently wiped out every
// time the option list refreshes, with no error of any kind.
✅ Fix: Re-Apply Selection to the New Nodes Explicitly, Every Rebuild
- Capture the currently selected values (Array.from(select.selectedOptions).map(o => o.value)) BEFORE clearing the option list, then set .selected = true on the matching new option nodes right after they’re created – selection has to be explicitly re-applied to each new node, because the browser has no way to know a brand-new option element with value 5 is ‘the same’ as the one it replaced.
- If the rebuild happens frequently (live search, live filtering), update or reuse existing <option> elements in place where their underlying data hasn’t changed, instead of clearing and recreating the whole list every time – this preserves selection state automatically because the nodes the user actually selected are never destroyed to begin with.
- For anything beyond a simple static list, consider whether a native multi-select is still the right control at all – a custom multi-select built from checkboxes or a listbox pattern makes ‘selected’ a property you control directly in your own data model, instead of state that lives on DOM nodes you’re about to destroy.
🚨 Why This Is Dangerous
- Because nothing throws an error or even a warning, this bug is purely behavioral – a user can select five items, trigger a background refresh they didn’t even initiate deliberately (a periodic data sync, a sibling filter changing), and submit a form believing their five selections are still intact when the control has quietly reset to nothing selected.
A select’s selected state lives on the option element itself, not on its value – delete the element, and you’ve deleted the selection, no matter how identical the replacement looks.
