Created
          November 27, 2024 10:00 
        
      - 
      
 - 
        
Save chrisdone/5604c7aab6f5a49e90f80b081687b00e to your computer and use it in GitHub Desktop.  
    htmx trigger-href extension
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | // 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