skip to Main Content

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]"
}

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search