views:

25

answers:

1

How to make this code work? I don't see how i can reach div from within $.get callback.

$("<div/>", {
    text: "some text",
    click: function (e) {
        $.get("bar.php", function(data) {
             $(this).text(data); // doesn't work
    });
    }
}).appendTo("body");
+3  A: 

Create a variable inside the click handler that holds the reference to the DIV and use that variable inside the $.get callback.

$("<div/>", {
    text: "some text",
    click: function (e) {
        var $div = $(this);
        $.get("bar.php", function(data) {
             $div.text(data); // should work
    });
    }
}).appendTo("body");
Rafael