Skip to content

Instantly share code, notes, and snippets.

@chrisdone
Created November 27, 2024 10:00
Show Gist options
  • Save chrisdone/5604c7aab6f5a49e90f80b081687b00e to your computer and use it in GitHub Desktop.
Save chrisdone/5604c7aab6f5a49e90f80b081687b00e to your computer and use it in GitHub Desktop.
htmx trigger-href extension
// Define the custom HTMX extension
htmx.defineExtension('trigger-href', {
onEvent: function(name, evt) {
// <https://htmx.org/events/#htmx:afterProcessNode>
if (name == 'htmx:afterProcessNode') {
const es = evt.detail.elt.querySelectorAll('a[hx-trigger-href]');
for (const e of es) {
if (typeof e.href == 'string') {
e.addEventListener('click',(e) => {
e.stopPropagation();
e.preventDefault();
})
}
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment