9

I need to add a class on after an ajax load. I first give a few elements a class "ready" which initiate a css transition. When the link li#menu-item-318 a gets clicked it removes the ready class which then reverses the css transition and then loads a new html document. On the Aja load I once again want to add the ready class to the same elements inserted by the Ajax call.

The code below has a callback to add the ready class, which works. But when the Ajax loads its sets the Ready class too early so there is no transition, even though my lines that is supposed to be drawn up is set.

I was thinking Its better I have a script for setting the classes on my transition elements inside the html that gets called by ajax to achieve my desired effect - but that doesn't work. So how do I do?

Demo: http://svensson.streetstylizm.com/ Click the photography - Polaroid link to se how it reverses the animation, loads the page and then just show the lines.

Code:

$(function () {
    $('.v-line, .h-line, .nav, #ban_image img').addClass('ready');
});

$('li#menu-item-318 a').click(function (e) {
    e.preventDefault();
    var linkNode = this;
    $('.v-line, .h-line, #ban_image img')
        .removeClass('ready')
        .one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend',

    function (e) {
        $(".js-pageTransition").load("photo.html .test> *");
    });
});
2
  • The comment you made on the answer suggests that you asked the wrong question... Commented Sep 13, 2015 at 17:25
  • Yeah it seems so. My logic falied here. Commented Sep 13, 2015 at 17:31

2 Answers 2

18

You can use ajaxComplete():

$.ajaxComplete(function () {
  // Something to execute after AJAX.
});

Have this as an example:

$( document ).ajaxComplete(function( event,request, settings ) {
  $( "#msg" ).append( "<li>Request Complete.</li>" );
});

As said by Popnoodles, this executes when any AJAX call completes. So if you are looking for executing the code in one particular AJAX call, you need to use the success function.

Sign up to request clarification or add additional context in comments.

2 Comments

@user3344734 just bear in mind that this function is executed EVERY TIME ANY ajax call finishes. If you want it to happen in only one instance, you should use the success callback.
This worked, but gave the same result as the Ajax callback. This is so annoying. I literally have no idea why it wont load the elements and the css without the ready class first - its skips the whole animation. I think im out of ideas. This is the correct answer anyhow, thanks again.
4

The way to do this would be use the ajaxcomplete function in jQuery which is called implicitly after the completion of any ajax call.

The examples in this link should get you started

.ajaxcomplete() Function

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.