Recall jQuery event on ready

Im trying to get some code to center on .ready(), however when there is anything injected by AJAX I want it to recall the method. So Ive tried to use the following:-
$(document).ready(function () {
    $(document).on("ready",".centered",function () {
      $(".centered").center();
    });
});

Open in new window


But it doenst center the document at all, not even at first run.

I know the code works, as if I use below it works great, but doesnt call again when I inject HTML via an AJAX call:-
$(document).ready(function () {
      $(".centered").center();
});

Open in new window


Any ideas how I can keep refreshing this code?

Thank you in advance
tonelm54Asked:
Who is Participating?
 
skijCommented:
You should consider adding
      $(".centered").center();
to the AJAX callback function.
0
 
skijCommented:
If you are unable to add that to the AJAX callback function, you could create a timed function to do this in the background:
$(document).ready(function () {
     $(".centered").center();
     setTimeout(function(){ 
        $(".centered").center();
     }, 1000);
});

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.