Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery .load() callback function

Tags:

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.

like image 527
Dan Avatar asked Oct 20 '10 00:10

Dan


People also ask

What is the use of jQuery load method?

The jQuery load() method is a simple, but powerful AJAX method. The load() method loads data from a server and puts the returned data into the selected element.

Is jQuery load deprecated?

The load() method was deprecated in jQuery version 1.8 and removed in version 3.0. Use the on() or trigger() method instead.

What is the use of callback function in jQuery?

jQuery Callback FunctionsJavaScript statements are executed line by line. However, with effects, the next line of code can be run even though the effect is not finished. This can create errors. To prevent this, you can create a callback function.

Is jQuery load async?

From what I know, the load event will always fire asynchronously, except if the image is already cached (in some browsers). The only reliable solution is to put the code in a callback like you did.


2 Answers

$("#bdiv").load("bosses.php #icc10n",function(data){     // use the data param     // e.g. $(data).find('#icc10n') }); 
like image 140
Petah Avatar answered Oct 09 '22 11:10

Petah


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.

like image 28
Danny Bullis Avatar answered Oct 09 '22 10:10

Danny Bullis