Friday, April 29, 2011

Prepending and animating text loaded via AJAX with jQuery.

Old hand with Prototype, new to jQuery, and writing a simple app to get a feel for the framework (and because I want to use it). I've got an HTML fragment that I load via AJAX, and I want to stick this at the top of a div, with a slide-in transition animation.

This bit works, and does the prepending bit:

// Get the HTML fragment and stick it at the top of the containing div.
$.post("/create_new_thing", function(data) {
    $('#container_div').prepend(data);
});

What I'd like to do, and can't figure out, is animate the newly added HTML fragment with a show() effect.

Any suggestions?

From stackoverflow
  • Try something like this...

    $('#div').load('file.html').fadeIn("slow");
    

    The load function is better suited to your needs, as it's main purpose is to load HTML from a remote file and inject it into the DOM.

    Using the "post" function is better for loading a remote page using a POST request (posting data via a form and returning dynamic data based on your post request).

    See below...

    $.post("file.php", { name: "superuntitled", time: "2am" },
    function(data){
      $('#div').fadeIn("slow").append(data);
    });
    

    jQuery has no support yet for "PUT" requests. So if you really need to use a put request, I can recommend extending the jQuery functionality with a custom function that adds support for "PUT". That said, there are some work arounds! See here for more details! ... http://homework.nwsnet.de/news/9132_put-and-delete-with-jquery

    Don Werve : I'll give this a go, but I can see cases where I'll want to do this via a POST request -- I just didn't put any parameters in my example because I want to keep it simple. Thanks, though!
    Don Werve : Whoops, not POST, PUT.
    superUntitled : i am not sure what "PUT" is?
    moff : @superUntitled: you may read about the put request over at w3c: http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.6
    superUntitled : jQuery has no support yet for "PUT" requests. So if you really need to use a put request, I can recommend extending the jQuery functionality with a custom function that adds support for "PUT". See here for more details! http://homework.nwsnet.de/news/9132_put-and-delete-with-jquery

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.