Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
HTML

HTML5: Fix a Native Multi-Select Dropdown Silently Losing Every Selected Option When Its Option List Is Rebuilt From JavaScript

- 23.10.26 - ErcanOPAK

🧩 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.

— Frontend Architect

Related posts:

HTML: Master Responsive Images with Picture Element

HTML: Master Responsive Images with Picture Element

HTML: Use Picture Element and srcset for Responsive Images

HTML: Use Picture Element and srcset for Responsive Images

HTML5: Fix a Video That Won't Autoplay on Mobile Browsers

HTML5: Fix a Video That Won't Autoplay on Mobile Browsers

Views: 8

Post navigation

WordPress: Fix Shortcodes Rendering Twice Because a Template Calls do_shortcode() Manually Inside Content WordPress Already Runs Through the_content
The AI Prompt That Reviews Your Code for a Bulk Delete or Update Running Without a Transaction, Leaving Data Half-Changed on Failure

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (980)
  • How to add default value for Entity Framework migrations for DateTime and Bool (976)
  • Get the First and Last Word from a String or Sentence in SQL (857)
  • How to select distinct rows in a datatable in C# (852)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (653)
  • Add Constraint to SQL Table to ensure email contains @ (607)
  • Average of all values in a column that are not zero in SQL (569)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (546)
  • Find numbers with more than two decimal places in SQL (475)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit
  • SQL Server: Fix ROW_NUMBER Ties Reordering Between Identical Runs
  • C#: Fix an Enum HasFlag Returning True for the Zero Value
  • WordPress: Fix Featured Images Vanishing After a CDN Migration
  • Photoshop: Fix Content-Aware Fill Smearing a Repeating Pattern
  • Asp.Net Core: Fix Antiforgery Tokens Rejected Behind a Reverse Proxy

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,055)
  • Get the User Name and Domain Name from an Email Address in SQL (980)
  • How to add default value for Entity Framework migrations for DateTime and Bool (976)
  • Get the First and Last Word from a String or Sentence in SQL (857)
  • How to select distinct rows in a datatable in C# (852)

Recent Posts

  • C#: Fix a ConditionalWeakTable Entry Vanishing Too Soon
  • Docker: Fix a Stale COPY Layer After Editing .dockerignore
  • Photoshop: Fix a Pattern Overlay Scaling Wrong on a High-DPI File
  • SQL Server: Fix a Computed Column Index Skipped by Implicit Cast
  • HTML5: Fix a required Input Inside a Hidden Tab Blocking Submit

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com