tags:

views:

35

answers:

2

I've seen lots of questions and solutions to problems like this but nothing has worked for me. I have this:

function() {
    $("#bdiv").load("bosses.php #icc10n",function(){
        return $("#bdiv").html();
    });
}

But it's not working. To clarify, I want to load content into #bdiv and then return the contents of #bdiv. But it seems that $("#bdiv").html() is being returned before the content is loaded even though I've put it in a callback function.

A: 

You can't do that.

AJAX is asynchronous, meaning that your function will return before the server sends a response.
You need to pass the value to your caller using a callback, the way $.load does.

SLaks
@Onkelborg: He needs to return the value by making his own callback.
SLaks
The .html() is evaluated when the function is declared, and not when it's called, right?
Santiago Lezica
@Santiago: Totally wrong. The `return` statement is returning from the inner callback method; this return value is ignored by jQuery. The outer function returns immediately.
SLaks
Call me slow, but I still don't really understand how it's done and why the above approach doesn't work. I know this is not my question, but would you mind adding an example and/or explaining a bit further? This bit my curiosity!
Santiago Lezica
+1  A: 
$("#bdiv").load("bosses.php #icc10n",function(data){
    // use the data param
    // e.g. $(data).find('#icc10n')
});
Petah
thanks, working beatifully now!
Dan