Tags
jQuery
Asked 2 years ago
24 Jun 2021
Views 380
Kali

Kali posted

Call jQuery function after ajax html get loaded

Call jQuery function after ajax html get loaded
Phpworker

Phpworker
answered Apr 28 '23 00:00

To call a jQuery function after an AJAX HTML content is loaded, you can use the success callback of the $.ajax method or the load method.

Here's an example using the $.ajax method:



<div id="content">
  <!-- This content will be replaced via AJAX -->
</div>

<button id="load-content">Load Content</button>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
  // Listen for the click event on the "Load Content" button
  $('#load-content').click(function() {
    // Make an AJAX request to load content into the #content element
    $.ajax({
      url: '/path/to/content',
      success: function(response) {
        // Replace the content of the #content element with the loaded content
        $('#content').html(response);
        
        // Call your function after the content is loaded
        yourFunction();
      }
    });
  });

  function yourFunction() {
    // Your function here
    console.log('Content loaded via AJAX');
  }
</script>

In this example, we use the success callback of the $.ajax method to call a function yourFunction () after the AJAX content is loaded.

You can replace the console.log statement in yourFunction () with your own custom code to perform any actions that you want to take after the AJAX content has been loaded.
Post Answer