Skip to content

Commit 1f24c6c

Browse files
committed
Fix changed trigger specifier with checkbox nodes
<input type="checkbox"> is special in that it shows up with a value of "on", even if it is not checked. Introduce a helper function for getting the effective value of a node by handling this pecularity.
1 parent 1e80780 commit 1f24c6c

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

src/htmx.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ var htmx = (function() {
332332
getAttributeValue,
333333
getClosestAttributeValue,
334334
getClosestMatch,
335+
getElementValue,
335336
getExpressionVars,
336337
getHeaders,
337338
getInputValues,
@@ -2456,6 +2457,17 @@ var htmx = (function() {
24562457
return false
24572458
}
24582459

2460+
/*
2461+
* @param {Element} elt
2462+
*/
2463+
function getElementValue(elt) {
2464+
if (elt.tagName.toLowerCase() === 'input' && elt.getAttribute('type') === 'checkbox') {
2465+
return elt.checked ? elt.value : ''
2466+
}
2467+
// @ts-ignore value will be undefined for non-input elements, which is fine
2468+
return elt.value
2469+
}
2470+
24592471
/**
24602472
* @param {Node} elt
24612473
* @param {Event|MouseEvent|KeyboardEvent|TouchEvent} evt
@@ -2512,8 +2524,7 @@ var htmx = (function() {
25122524
if (!elementData.lastValue.has(triggerSpec)) {
25132525
elementData.lastValue.set(triggerSpec, new WeakMap())
25142526
}
2515-
// @ts-ignore value will be undefined for non-input elements, which is fine
2516-
elementData.lastValue.get(triggerSpec).set(eltToListenOn, eltToListenOn.value)
2527+
elementData.lastValue.get(triggerSpec).set(eltToListenOn, getElementValue(eltToListenOn))
25172528
})
25182529
}
25192530
forEach(eltsToListenOn, function(eltToListenOn) {
@@ -2556,8 +2567,7 @@ var htmx = (function() {
25562567
}
25572568
if (triggerSpec.changed) {
25582569
const node = evt.target
2559-
// @ts-ignore value will be undefined for non-input elements, which is fine
2560-
const value = node.value
2570+
const value = getElementValue(node)
25612571
const lastValue = elementData.lastValue.get(triggerSpec)
25622572
if (lastValue.has(node) && lastValue.get(node) === value) {
25632573
return

test/attributes/hx-trigger.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,22 @@ describe('hx-trigger attribute', function() {
4040
div.innerHTML.should.equal('Requests: 1')
4141
})
4242

43+
it('changed modifier works for checkboxes', function() {
44+
var requests = 0
45+
this.server.respondWith('GET', '/test', function(xhr) {
46+
requests++
47+
xhr.respond(200, {}, 'Requests: ' + requests)
48+
})
49+
var input = make('<input type="checkbox" hx-trigger="click changed" hx-target="#d1" hx-get="/test"/>')
50+
var div = make('<div id="d1"></div>')
51+
input.click()
52+
this.server.respond()
53+
div.innerHTML.should.equal('Requests: 1')
54+
input.click()
55+
this.server.respond()
56+
div.innerHTML.should.equal('Requests: 2')
57+
})
58+
4359
it('changed modifier works along from clause with single input', function() {
4460
var requests = 0
4561
this.server.respondWith('GET', '/test', function(xhr) {

0 commit comments

Comments
 (0)