Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/htmx.js
Original file line number Diff line number Diff line change
Expand Up @@ -2430,9 +2430,11 @@ var htmx = (function() {
if (elt.tagName === 'FORM') {
return true
}
// find button wrapping the event elt
const btn = elt.closest('input[type="submit"], button')
// @ts-ignore Do not cancel on buttons that 1) don't have a related form or 2) have a type attribute of 'reset'/'button'.
// The properties will resolve to undefined for elements that don't define 'type' or 'form', which is fine
if (elt.form && elt.type === 'submit') {
if (btn && btn.form && btn.type === 'submit') {
return true
}
elt = elt.closest('a')
Expand Down
22 changes: 22 additions & 0 deletions test/core/regressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@
div.classList.length.should.equal(0)
done()
})

Check failure on line 323 in test/core/regressions.js

View workflow job for this annotation

GitHub Actions / test_suite

Trailing spaces not allowed
it('a button clicked inside an htmx enabled link will prevent the link from navigating on click', function(done) {
var defaultPrevented = 'unset'
var link = make('<a href="/foo" hx-get="/foo"><button>test</button></a>')
Expand Down Expand Up @@ -363,4 +363,26 @@
button.click()
})

it('a htmx enabled button containing sub elements will prevent the button submitting a form', function(done) {
var defaultPrevented = 'unset'
var form = make('<form><button hx-get="/foo"><span>test</span></button></form>')
var button = form.firstChild
var span = button.firstChild

htmx.on(button, 'click', function(evt) {
// we need to wait so the state of the evt is finalized
setTimeout(() => {
defaultPrevented = evt.defaultPrevented
try {
defaultPrevented.should.equal(true)
done()
} catch (err) {
done(err)
}
}, 0)
})

span.click()
})

Check failure on line 386 in test/core/regressions.js

View workflow job for this annotation

GitHub Actions / test_suite

Block must not be padded by blank lines

})
Loading