Search code examples
jqueryjs-scrollintoview

scrollIntoView() displays "not a function" in jquery


I have an html class that has several dozen divs each of which have the crib-song-name css class. When one of those elements are clicked, I want the browser to scroll to the next element that has that class type.

$(".crib-song-name").click(function () {
    var next = $(this).parent().next().find(".crib-song-name");
    if (next) {
        next.scrollIntoView(true);
    }
});

The click() and find() functions appear to be finding the correct (i.e., next) element. However, the debugger reports that scrollIntoView is not a function on this line:

next.scrollIntoView(true);

Guidance? Thanks!


Solution

  • The solution, as stated in a comment by @zer00ne, was to dereference my next variable, i.e., change this:

    next.scrollIntoView(true);
    

    to this:

    next[0].scrollIntoView(true);