0

I have the following coldes to replace all .replaceclass classes with something else, and it's working fine.

$('.replaceclass').replaceWith(function() {
    return 'string to be replaced';
}); 

But now I have some .replaceclass elements that were not generatede on first load, but may be loaded by ajax later, I also want them to get replaced every time when it's loaded.

I tried:

$('.replaceclass').on('load', function(){
    $('.replaceclass').replaceWith(function() {
        return 'string to be replaced';
    }); 
});

But it's not working. How can I achieve the desired result? Thank you very much!

1
  • 1
    Try to do replacement inside your ajax complete callback?
    – Resolution
    Jan 28, 2015 at 9:10

2 Answers 2

1

You can use $.ajaxSuccess which attaches a function to be executed whenever an Ajax request completes successfully.

$(document).ajaxSuccess(function(){
   $('.replaceclass').replaceWith(function() {
      return 'string to be replaced';
   }); 
});

This is a global event and gets triggered everytime there is a successful ajax operation. If you don't want a specific ajax call to trigger it, simply add global: false in its options.

0

What about that?

$.ajax({
  ......
 }).done(function() {
   replaceClasses();
 });

function replaceClasses(){
  $('.replaceclass').replaceWith(function() {
    return 'string to be replaced';
  }); 
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.