How to get element selector if div is loaded dynamically on ajax call

I need to fire two ajax calls. The second call loads some html on the page. But on the first call, I need to process one div that won't be there until the second call completes. Hence it gives an error as the selector is null. What is the solution to this. Can I change the sequence of the two calls.

+3


source to share


2 answers


Why not just handle the callback? For example:



$.ajax({
  url: "test.html",
  context: document.body
}).done(function() {
  $( this ).addClass( "done" );
});

      

+2


source


you need to check the length of the selector

as below



if($('selector').length){     // selector "ID" or 'Class'
  /// your code
}

      

NOTE. If the selector is available after the second ajax call, you can write your code in the second function of the successful (callback) ajax call function.

+1


source







All Articles