-1

So, i have a function that is called via a selector

$(window).scroll(function() {
    console.log($("#content div").inview());
});

However it returns undefined. When I return $elems it shows me all correct elements, but when i return $el it gives undefined.

$.fn.inview = function() {

    var $elems = this;

    function getViewportHeight() {
        /*do tsoomething that works*/
        return height;
    }

    var vpH = getViewportHeight(),
    scrolltop = (document.documentElement.scrollTop ?
        document.documentElement.scrollTop :
        document.body.scrollTop);

    // return $elems.toArray();
    $elems.each(function () {
        var $el = $(this)
        height = $el.height() - (vpH / 1.5),
        inview = $el.data('inview') || false,
        topofscreen = scrolltop,
        botofscreen = topofscreen + vpH,
        topofelement= $el.offset().top;

        return $el;


};
jjbbss
  • 139
  • 2
  • 15
  • Possible duplicate of [Return a value when using jQuery.each()?](http://stackoverflow.com/questions/3820269/return-a-value-when-using-jquery-each) – JJJ Oct 30 '15 at 11:16
  • 1
    `return $elems.each(...);` from $.fn.inview() – A. Wolff Oct 30 '15 at 11:19

1 Answers1

2

inview doesn't have a return statement, so it always returns undefined

When you return $el; you are doing it inside the anonymous function you pass to $elems.each

If you want to return something from inview then you need a return statement in that function. Possibly you want to create an array just before you call each, push $el onto the array each time you go around the loop and then return the array.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335