Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery document ready after ajax request

I'm having problems with updating elements that are not ready after an ajax request.

If I run my myFunction() function on page load like so:

$(function() {
 myFunction();
}

I have no problems at all. But if I then use something like

$.ajax({
      url: this.href,
      dataType: "script",
      complete: function(xhr, status) {
        myFunction();
      }
    });

which returns $(".myElement").replaceWith("htmlHere"). The elements are simply not ready when the complete event fires. If I set a delay in there it works fine again.

Is there any other event that gets fired other than 'complete' when the DOM is ready?

Update:

Here's the actual code:

$(function() {
  $("a.remote").live("click", function(e) {    
    $.ajax({
      url: this.href,
      dataType: "script",
      success: function(xhr, status) {
                myFunction();
            }
    });

    e.preventDefault();
    return false;
  });

  myFunction();
});

function myFunction() {
    // Modify the dom in here
}

The missing ); was just a typo on my part.

Ive tried using success now instead of complete and it doesn't appear to make any difference.

like image 465
Danny Avatar asked Sep 23 '11 12:09

Danny


People also ask

How do I run Ajax in document ready?

Create an HTML document that includes the jQuery library. Inside a script element in your HTML document, write jQuery's ready() function, which will wait until the selected object (the HTML document in this case) is ready before executing the code passed into it.

How can write document ready function in jQuery?

$( document ).ready() Code included inside $( window ).on( "load", function() { ... }) will run once the entire page (images or iframes), not just the DOM, is ready. // A $( document ).ready() block. console.log( "ready!" );

Where do you put document ready?

So technically it doesn't matter where you put it. Many people like putting script in the head, because it makes sure the script is read before the page is loaded. Other people like putting it at the very end (just before the end body tag) so that all of the elements of the page are loaded before the script reads them.

What does $( document .ready function () mean?

The ready() method is used to make a function available after the document is loaded. Whatever code you write inside the $(document ). ready() method will run once the page DOM is ready to execute JavaScript code.


2 Answers

I have set up a jsfiddle based on your code, and it seems to be working.

This is the current code:

 $(function() {
  $("a.remote").live("click", function(e) {    
    $.ajax({
      url: this.href,
      dataType: "script",
      success: function(xhr, status) {
                myFunction();
            }
    });

    e.preventDefault();
    return false;
  });

});

function myFunction() {
    $("span").replaceWith("<p>test</p>");
}

And it replaces span tag with a paragraph. Please check it and compare with your code. If it is the same, then your problem somewhere other than this function (maybe in myFunction?).

like image 77
Goran Obradovic Avatar answered Oct 21 '22 11:10

Goran Obradovic


You can use $(document).ready(function() { ... }); to wrap up anything you want fired when the DOM has loaded. Your ajax request could be placed inside the document.ready if you want this to wait until the dom has loaded.

If you want to wait until the ajax has loaded its resource then you should use ajax.success rather than complete.

like image 30
Timbo Avatar answered Oct 21 '22 11:10

Timbo