diff --git a/src/compiler/compile/render_dom/wrappers/Element/Attribute.ts b/src/compiler/compile/render_dom/wrappers/Element/Attribute.ts index 3f57d004be..775a36fc56 100644 --- a/src/compiler/compile/render_dom/wrappers/Element/Attribute.ts +++ b/src/compiler/compile/render_dom/wrappers/Element/Attribute.ts @@ -140,6 +140,14 @@ export default class AttributeWrapper extends BaseAttributeWrapper { block.chunks.hydrate.push( b`${element.var}.${property_name} = ${init};` ); + const is_selected = property_name === 'selected'; + const is_boolean = typeof init.value === 'boolean'; + const attr_val = is_boolean ? x`""` : init; + if (is_selected) { + block.chunks.hydrate.push( + b`${method}(${element.var}, "${name}", ${attr_val});` + ); + } updater = block.renderer.options.dev ? b`@prop_dev(${element.var}, "${property_name}", ${should_cache ? this.last : value});` : b`${element.var}.${property_name} = ${should_cache ? this.last : value};`; diff --git a/src/runtime/internal/dom.ts b/src/runtime/internal/dom.ts index eb3389e3f8..4aafcc0467 100644 --- a/src/runtime/internal/dom.ts +++ b/src/runtime/internal/dom.ts @@ -534,16 +534,17 @@ export function set_style(node, key, value, important) { } export function select_option(select, value) { + let have_select_attr = false; for (let i = 0; i < select.options.length; i += 1) { const option = select.options[i]; - + if (option.hasAttribute('selected')) have_select_attr = true; if (option.__value === value) { option.selected = true; return; } } - select.selectedIndex = -1; // no option should be selected + if (!have_select_attr) select.selectedIndex = -1; // no option should be selected } export function select_options(select, value) {