You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Possible Issue: Using hx-swap-oob="innerHTML" on a <select> element ignores the selected attribute, while hx-swap-oob="outerHTML" respects it.
Summary
When replacing the options of a <select> using HTMX’s out-of-band swaps:
outerHTML version works as expected: the <option selected> appears selected in the browser.
innerHTML version ignores the selected attribute, causing the last <option> to be selected instead.
I’ve added debug JavaScript that reveals the DOM selected property is always set to false for the newly swapped <option> elements when using innerHTML.
I do have a workaround by switching to hx-swap-oob="outerHTML", but would prefer using innerHTML if at all possible. Any hints or solutions for this would be greatly appreciated.
Minimal Reproduction
At the very bottom, below, are two minimal FastHTML examples, but the underlying issue likely belongs to how HTMX handles innerHTML swaps on <select>:
Observations / Debug
When using outerHTML, the newly introduced selected attribute immediately becomes .selected = true in the DOM.
When using innerHTML, the browser’s final DOM ends with .selected = false for that <option>, and the last <option> ends up selected.
Console logs from htmx.logAll() plus htmx:afterSwap show the correct HTML snippet arriving, but the actual <select> element in the final DOM is ignoring the selected attribute.
Questions / Request
Is there a known workaround for preserving selected with hx-swap-oob="innerHTML"?
Why might the <option> be reset in this scenario?
I’d appreciate any insight or suggestions for a fix or alternative approach—thanks in advance!
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Possible Issue: Using
hx-swap-oob="innerHTML"
on a<select>
element ignores theselected
attribute, whilehx-swap-oob="outerHTML"
respects it.Summary
When replacing the options of a
<select>
using HTMX’s out-of-band swaps:outerHTML
version works as expected: the<option selected>
appears selected in the browser.innerHTML
version ignores theselected
attribute, causing the last<option>
to be selected instead.I’ve added debug JavaScript that reveals the DOM
selected
property is always set tofalse
for the newly swapped<option>
elements when usinginnerHTML
.I do have a workaround by switching to
hx-swap-oob="outerHTML"
, but would prefer usinginnerHTML
if at all possible. Any hints or solutions for this would be greatly appreciated.Minimal Reproduction
At the very bottom, below, are two minimal FastHTML examples, but the underlying issue likely belongs to how HTMX handles
innerHTML
swaps on<select>
:Observations / Debug
outerHTML
, the newly introducedselected
attribute immediately becomes.selected = true
in the DOM.innerHTML
, the browser’s final DOM ends with.selected = false
for that<option>
, and the last<option>
ends up selected.htmx.logAll()
plushtmx:afterSwap
show the correct HTML snippet arriving, but the actual<select>
element in the final DOM is ignoring theselected
attribute.Questions / Request
selected
withhx-swap-oob="innerHTML"
?<option>
be reset in this scenario?I’d appreciate any insight or suggestions for a fix or alternative approach—thanks in advance!
app_inner.py
:app_outer.py
:Beta Was this translation helpful? Give feedback.
All reactions