23

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.

1
  • Can you show the full code? Why is this in an anonymous function? Commented Oct 20, 2010 at 1:06

3 Answers 3

53
$("#bdiv").load("bosses.php #icc10n",function(data){
    // use the data param
    // e.g. $(data).find('#icc10n')
});
Sign up to request clarification or add additional context in comments.

Comments

1

as far as I know you cannot make a return statement in the callback function of a $.ajax(), $.post(), $.get(), etc.. method. You could, however, store the 'data' value in a variable declared outside the function, and then set the value of that variable when the callback function executes. And there is a variety of other options.

Comments

0

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.

4 Comments

@Onkelborg: He needs to return the value by making his own callback.
The .html() is evaluated when the function is declared, and not when it's called, right?
@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.
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.