November 8, 2014 · javascript ajax jquery

Ajax-based events with jQuery


Sometimes needed to fire events after ajax request was made. For example, for dynamically generated forms.
There are several useful AjaxEvents, detailed described here.

For example, do something after ajax request

$(document).ajaxComplete(function() {
  console.log('right after ajax');
  //do stuff
});

another one

$(document).ajaxComplete(function() {
 $('label.valign').each(function() {
        text = $(this).html().replace(/\(/,'</br><p class=\"delcomment\">').replace(/\)\)/,')</p>').replace(/-.{1,}/g, '');
        return $(this).html(text);
 })
});

Another example from jquery.com

$( document ).ajaxComplete(function( event, xhr, settings ) {
  if ( settings.url === "ajax/test.html" ) {
   $( ".log" ).text( "Triggered ajaxComplete handler. The result is " + xhr.responseText );
  }
});
  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket
Comments powered by Disqus