Home > Back-end >  How to execute javascript code after htmx makes an ajax request?
How to execute javascript code after htmx makes an ajax request?

Time:08-18

Im currently building a website with django and htmx and i like the combination so far. Lets say I use an htmx attribute on a button to replace a div in the DOM with another div that is supposed to contain a wysiwyg editor. Now the wysiwyg editor has to be initialized with javascript. How do I do this? Can I just return the script tag under the editor div that is being requested with htmx? Wouldnt that be also a little ugly or bad practice because youd have script tags in the middle of the html body? Whats the best way of solving this? Thanks in advance

CodePudding user response:

Check out HTMX after request event.

It should look something like this

htmx.on('afterRequest', (evt) => {
  // check which element triggered the htmx request. If it's the one you want call the function you need
})
  • Related