Skip to content

Commit 075ed73

Browse files
scrhartleyscrhartley
andauthored
Fix target value in htmx:targetError when inheritance has been used (#3178)
Co-authored-by: scrhartley <scrhartley@github.com>
1 parent e4ecc55 commit 075ed73

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

src/htmx.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4168,7 +4168,7 @@ var htmx = (function() {
41684168
}
41694169
const target = etc.targetOverride || asElement(getTarget(elt))
41704170
if (target == null || target == DUMMY_ELT) {
4171-
triggerErrorEvent(elt, 'htmx:targetError', { target: getAttributeValue(elt, 'hx-target') })
4171+
triggerErrorEvent(elt, 'htmx:targetError', { target: getClosestAttributeValue(elt, 'hx-target') })
41724172
maybeCall(reject)
41734173
return promise
41744174
}

test/core/events.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,4 +738,36 @@ describe('Core htmx Events', function() {
738738
htmx.off('htmx:afterSwap', afterSwapHandler)
739739
}
740740
})
741+
742+
it('htmx:targetError should include the hx-target value', function() {
743+
var target = null
744+
var handler = htmx.on('htmx:targetError', function(evt) {
745+
target = evt.detail.target
746+
})
747+
try {
748+
this.server.respondWith('GET', '/test', '')
749+
var div = make('<div hx-post="/test" hx-target="#non-existent"></div>')
750+
div.click()
751+
this.server.respond()
752+
target.should.equal('#non-existent')
753+
} finally {
754+
htmx.off('htmx:targetError', handler)
755+
}
756+
})
757+
758+
it('htmx:targetError can include an inherited hx-target value', function() {
759+
var target = null
760+
var handler = htmx.on('htmx:targetError', function(evt) {
761+
target = evt.detail.target
762+
})
763+
try {
764+
this.server.respondWith('GET', '/test', '')
765+
make('<div hx-target="#parent-target"><div id="child" hx-post="/test"></div></div>')
766+
byId('child').click()
767+
this.server.respond()
768+
target.should.equal('#parent-target')
769+
} finally {
770+
htmx.off('htmx:targetError', handler)
771+
}
772+
})
741773
})

0 commit comments

Comments
 (0)