Introducing ReUI:Open-source UI components and apps built with React, Next.js and Tailwind CSS
Browse ReUI

Lost functionality after HTML reloaded with AJAX call


Hi! Couldn't find an answer on this in here or stackoverflow, but I guess is something common.

I'm loading some content dynamically using jQuery $.get function.

If you have for example $('.element').click(some function); and you reload the element with AJAX, that directive get lost. You can easily make it work by attaching the function to the body instead: $('body').on('click', '.element', some function);

But I don't know how to replicate that with Metronic managed functions in scripts.bundle.js. Specifically I'm reloading some HTML with data-kt-menu-trigger="click" inside, and that functionality get lost.

Any help would be much appreciated!


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (4)


Losing functionality after reloading HTML with an AJAX call can be frustrating. I’ve resolved this by ensuring that used trimble target id event handlers and scripts are correctly reinitialized after the AJAX call completes. Debugging the JavaScript code and checking for any issues with the HTML elements that are being dynamically loaded also helps.



Hi,

Please call KTComponents.init() method to initialize newly populated components via Ajax on Ajax complete event.

For more info please check this guide.

Regards.



Worked like a charm! Thanks for the quick answer.



Hi happy,

Glad to help you out. All the best with your project!

Regards.


Your Support Matters!

We will highly appreciate your Metronic Review on Themeforest.
Please go to Themeforest Downloads page, select Metronic & leave your feedback.
Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(