diff --git a/src/htmx.js b/src/htmx.js index 69348460f..8d9181c11 100644 --- a/src/htmx.js +++ b/src/htmx.js @@ -2435,7 +2435,9 @@ var htmx = (function() { if (elt.form && elt.type === 'submit') { return true } - if (elt instanceof HTMLAnchorElement && elt.href && + elt = elt.closest('a') + // @ts-ignore check for a link wrapping the event elt or if elt is a link. elt will be link so href check is fine + if (elt && elt.href && (elt.getAttribute('href') === '#' || elt.getAttribute('href').indexOf('#') !== 0)) { return true } diff --git a/test/core/internals.js b/test/core/internals.js index e3d4f96df..9b6d0b97c 100644 --- a/test/core/internals.js +++ b/test/core/internals.js @@ -107,6 +107,11 @@ describe('Core htmx internals Tests', function() { htmx._('shouldCancel')({ type: 'submit', target: anchorThatShouldNotCancel }, form).should.equal(false) htmx._('shouldCancel')({ type: 'click', target: divThatShouldNotCancel }, form).should.equal(false) + // check elements inside links getting click events should cancel parent links + var anchorWithButton = make("") + htmx._('shouldCancel')({ type: 'click', target: anchorWithButton.firstChild }, anchorWithButton).should.equal(true) + htmx._('shouldCancel')({ type: 'click', target: anchorWithButton.firstChild }, anchorWithButton.firstChild).should.equal(true) + form = make('