-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;


};
2
  • Possible duplicate of Return a value when using jQuery.each()? Commented Oct 30, 2015 at 11:16
  • 1
    return $elems.each(...); from $.fn.inview() Commented Oct 30, 2015 at 11:19

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.