Adam
Adam

Reputation: 20882

jquery trigger function from out

I have the following Jquery function.

I want to know how I can call it from outside the function. There are occasions where I need to run it. I can see how to trigger/call it from inside the function at the bottom but I'm unsure how to from outside of it.

$.fn.lazyload = function() {
    var elem = this;

    var lazyload = function(elem) {
        var viewport_height = $(window).height() + 100;
        var scrollTop = $(document).scrollTop();
    }
    lazyload(elem);
    $(window).scroll(function(){lazyload(elem);});
    $(window).resize(function(){lazyload(elem);});
};

I've tried the following but they don't work:

$.fn.lazyload(elem); 
lazyload(elem);
lazyload();

thanks

Upvotes: 4

Views: 38

Answers (1)

putvande
putvande

Reputation: 15213

You can use apply for that:

$.fn.lazyload.apply(elem);

Assuming elem is the element you want the function to work with.

Upvotes: 2

Related Questions