I use Ajax to create dynamically something like 100 div with a function called lambda() and then I want to scroll on one of the new div (id='new\_id') with Jquery but failed because when my

```
//lambda()//<--create $('#new_id') dynamically
$('html, body').animate({
    scrollTop: $('#new_id').offset().top
}, 500);

```

is fired, the DOM is not updated in time and the $('#new\_id') returns undefined.
So, I need something like .ready for this specific selector to tell me when the DOM is ready to be used.  

I know that .on must be used when you want to attach an event to a dynamic content but my problem is only for instructions immediately triggered with no event, just an immediate scrolling.

Thank you for your help.

EDIT: detailed & purified code:
-------------------------------

1/the chk() function is triggered with a submit event  

2/inside chk(), $.ajax will add some data in DB, let's say it's a div content  

3/if success, if the php doesn't return an error (data.err), it triggers my callback function lambda()  

4/lambda() will display the new div content + the old ones (=data.com) on $('#bloc\_com'), it's an update function  

5/when lambda() has finished, I want the animation to work to scroll up to $('#'+data.new\_id)

.ajaxComplete WORKS but $.when() + .done()/.then() NO!
However, I have others ajax functions on this page and .ajaxComplete is triggered for all of them and it's not what I want.

```
function lambda(p){
    var b=$('#bloc_com');
    var com_in=$('#com_in');

    $.get('ajax/com_pagination.php',{p:p},function(data)
        {
            b.html(data.com);//update the webpage
        },'json'
    ).fail(function() {
        com_in.html("<span class='erreur222'>Error system. If the problem persists, please contact the administration.</span>");
    });
    return false;
}

function chk(){
    var err=$('#err_com');

    $.when($.ajax({
        type:'post',
        url:'ajax/com_chk.php',
        data:{id:id,dt:time,cm:com},
        success:function(data)
        {
            err.html(data.err);//display error
            if(data.err=='')
            {
                var p=1;
                lambda(p);//callback function that will display the 100 div with the new one which this ajax call adds in DB

                //.ajaxComplete works here!
                /*$(document).ajaxComplete(function() {
                    $('html, body').animate({
                        scrollTop: $('#'+data.new_id).offset().top
                    }, 500);
                });*/
            }
        },
        dataType:'json'
    })).then(function() {
        alert('done method fired!');//it's working
        $('html, body').animate({
            scrollTop: $('#'+data.new_id).offset().top//return undefined
        }, 500);
    });
    return false;
}

```