Skip to content

Commit 8b0d346

Browse files
authored
Fix hx-live form reset state (#3933)
Fix hx-live form reset state
1 parent 9692e8d commit 8b0d346

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

src/ext/hx-live.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,11 @@
134134
: (value ? 'true' : 'false');
135135
e.setAttribute(name, attrVal);
136136
} else if (isPropAttr) {
137-
if (value === false || value == null) {
137+
if (name === 'checked' || name === 'selected') {
138+
let present = !!value;
139+
e[name] = present;
140+
e.toggleAttribute(name, present);
141+
} else if (value === false || value == null) {
138142
e[name] = (typeof e[name] === 'boolean') ? false : '';
139143
e.removeAttribute(name);
140144
} else if (value === true) {

test/tests/ext/hx-live.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1927,6 +1927,19 @@ describe('hx-live extension', function () {
19271927
mirror.hasAttribute('checked').should.equal(true);
19281928
});
19291929

1930+
it('keeps checked and selected false after form reset when the result is zero', function() {
1931+
playground().innerHTML = `
1932+
<form>
1933+
<input type="checkbox" :checked="0">
1934+
<select multiple><option :selected="0">One</option></select>
1935+
</form>
1936+
`;
1937+
htmx.process(playground());
1938+
playground().querySelector('form').reset();
1939+
playground().querySelector('input').checked.should.equal(false);
1940+
playground().querySelector('option').selected.should.equal(false);
1941+
});
1942+
19301943
it(':value syncs property and attribute', async function() {
19311944
playground().innerHTML = `
19321945
<input id="src" value="hello">

0 commit comments

Comments
 (0)