Uncaught Syntax error, unrecognized expression: [object Object]

Work in the current news scroller - see my live example here - EXAMPLE

When I click the next / prev arrow, I get an error log Uncaught Syntax error, unrecognized expression: [object Object]

Why is it problem? Where is the syntax error?

JQuery Code:

        (function($) {
    /*!  Scroller
        ---------------------------------------------*/
        $.fn.Scroller = function() {

            //Set height
            $('.scroller').each(function() {
                var height = 0;
                $(this).children('div').each(function() {

                    if (height < $(this).height()) {
                        height = $(this).height();
                    }

                });
                $(this).css("height", height + "px");

            });

            $('.scroller').each(function() {

                var NextArrow = $(this).parent().find('.next');
                var PrevArrow = $(this).parent().find('.prev');


                // Set a timeout
                var timeOut = setTimeout(nextNotice, 5000);

                // pause on hover
                $(this).hover(

                function() {
                    clearTimeout(timeOut);
                }, function() {
                    timeOut = setTimeout(nextNotice, 5000);
                });

                // Next notice function called on timeout or click
                //set a flag that use to be an oberver to listen when the fadeIn done
                var flag = true;

                function nextNotice(event) {

                    var CurrentScrollerDiv = $(this).parent().find('.scroller');

                    if (!flag) {
                        return false;
                    }
                    clearTimeout(timeOut);

                    flag = false;
                    timeOut = setTimeout(nextNotice, 5000);

                    if ($(CurrentScrollerDiv + ' div:visible').is(CurrentScrollerDiv + '  div:last-child')) {
                        $(CurrentScrollerDiv + ' div:visible').fadeOut(300);
                        $(CurrentScrollerDiv + ' div:first-child').fadeIn("slow", function() {
                            flag = true;
                        });
                    } else {
                        $(CurrentScrollerDiv + ' div:visible').fadeOut(300).next('div').fadeIn("slow", function() {
                            flag = true;
                        });
                    }
                    return false;
                }

                $(NextArrow).click(nextNotice);
                $(PrevArrow).click(function(event) {

                    var CurrentScrollerDiv = $(this).parent().find('.scroller');

                    if (flag) {
                        return false;
                    }
                    clearTimeout(timeOut);

                    flag = false;
                    timeOut = setTimeout(nextNotice, 5000);

                    if ($(CurrentScrollerDiv + ' div:visible').is(CurrentScrollerDiv + ' div:first-child')) {
                        $(CurrentScrollerDiv + ' div:visible').fadeOut(300);
                        $(CurrentScrollerDiv + ' div:last-child').fadeIn("slow", function() {
                            flag = true;
                        });
                    }
                    else {
                        $(CurrentScrollerDiv + ' div:visible').fadeOut(300).prev('div').fadeIn("slow", function() {
                            flag = true;
                        });
                    }
                    return false;

                });

            });

        };

    })(jQuery);


    $(document).ready(function() {
        //Blog
        $('.itBlog > div:first-child').show();

        //Scroller
        $('.scroller').Scroller();

    });
+5
source share
3 answers

To create selectors from existing objects, use the second parameter$ :

$('div:visible', CurrentScrollerDiv)

Or functionfind :

CurrentScrollerDiv.find('div:visible');

CurrentScrollerDiv is not a string, so it cannot be combined with a string to generate a selector argument based on the string.


jQuery( selector, [ context ]  )
    jQuery( selector, [context] )    <-- you want this one, and
    jQuery( element )                    `selector` is a string
    jQuery( elementArray )
    jQuery( jQuery object )
    jQuery()
jQuery( html, [ ownerDocument ]  )
    jQuery( html, [ownerDocument] )
    jQuery( html,props )
jQuery( callback  )
    jQuery( callback )
+16
source

This is the problem line:

if ($(CurrentScrollerDiv + ' div:visible').is(CurrentScrollerDiv + '  div:last-child')) {

CurrentScrollerDiv, .toString() , , . jQuery DOM. jQuery .find():

if (CurrentScrollerDiv.find('div:visible').is(CurrentScrollerDiv.find('div:last-child')) {

, , if.

+3

Here is the wrong selector:

var CurrentScrollerDiv = $(this).parent().find('.scroller');

$(CurrentScrollerDiv + ' div:visible') 

fix

var CurrentScrollerDiv = $(this).parent().find('.scroller');
$('div:visible', CurrentScrollerDiv);
+2
source