Skip to content Skip to sidebar Skip to footer

How To Pass The Jquery Element Inside A Function - Syntax

I need to pass jQuery as an argument in the first two functions, or the code inside initialiseSticky will not work. It does work for document.ready but not for ajaxComplete, my sy

Solution 1:

Javascript implements Closures so any inner function is able to visit the parent scope.

var foo = 'bar';

functionbaz() {
  console.log(foo); // => baz
}

Now, because your initialiseSticky is an inner function of the global scopeWindow and also jQuery is in this scope as window.jQuery you don't need to pass a jQuery reference to initialiseSticky because it is already able to access it.

note that $ is just an alias of jQuery.

Solution 2:

Your whole code is messed around a lot. I'll try to strip it down.

$(function() { 
    console.log('document ready- sticky');
    initialiseSticky($);
});
$(document).ajaxComplete(function () {
    console.log('ajax complete- sticky');
    initialiseSticky($);
});

functioninitialiseSticky($) {
//my code
}

Try if this works. Note: I'm not sure passing $ is even necessary.

Post a Comment for "How To Pass The Jquery Element Inside A Function - Syntax"