Home > Mobile >  Get Caller in callback JQuery
Get Caller in callback JQuery

Time:11-22

is there any way to get $('#div') inside callback function? $('#div') changes

var param = {
    a: 'haha'
}

$('#div').load('/foo.html', param, foo())

function foo() {
   var div = caller?  // this variable should be "$('#div')[0]"
}

CodePudding user response:

this will be the element that .load was called on, eg:

$('#div').load('/foo.html', param, function() { $(this).show() })

Your edit to the question translates as:

var fooResult = foo();
$('#div').load('/foo.html', param, fooResult);

So in this case, no, because foo() will have already been called.

CodePudding user response:

According to the documentation: https://api.jquery.com/load/#load-url-data-complete

The callback is fired once for each element in the jQuery collection, and this is set to each DOM element in turn.

So:

var $div = $(this); // the jQuery object representing the div
var div = this; // the HTMLElement representing the div
  • Related