1
votes

I got this jQuery code from here - Fiddle

$(document).ready(function () {
var $pagination = $('.qpagination');
var $lis = $pagination.find('li:not(#qprev, #qnext)');
$lis.filter(':gt(4)').hide();
$lis.filter(':lt(5)').addClass('active');

var $next = $('#qnext').click(function () {
    var idx = $lis.index($lis.filter('.active:last')) || 0;

    var $toHighlight = $lis.slice(idx + 1, idx + 6);
    if ($toHighlight.length == 0) {
        $prev.show();
        return;
    }

    $next.show();        
    $lis.filter('.active').removeClass('active').hide();
    $toHighlight.show().addClass('active')
});

var $prev = $('#qprev').click(function () {
    var idx = $lis.index($lis.filter('.active:first')) || 0;

    var start = idx < 4 ? 0 : idx - 4;
    var $toHighlight = $lis.slice(start, start + 5);
    if ($toHighlight.length == 0) {
        $prev.hide();
        return;
    }      

    $next.show();
    $lis.filter('.active').removeClass('active').hide();
    $toHighlight.show().addClass('active')
});

}); // close jquery

However, when I copied the code and adjusted it according to my need it seems that it doesn't work. I get Uncaught SyntaxError: Unexpected token error.

What seems to be the issue? & how can I fix it?

Edit: These are the errors I get from Chrome:

Undefined variable: pagination

Undefined variable: lis

Undefined variable: next

Undefined variable: toHighlight

Edit#2: I got the code to work after loading jQuery 2.2.1 on fiddle but it doesn't work on

https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js?ver=4.4.2

in Wordpress, this version is used. http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js?ver=4.4.2

it doesn't work on that one neither, How could I get it to work on ver=4.4.2?

Here is my live website: http://gulf-brokers.com/

2
A syntax error is a syntax error. It should also include a line/character number. Start by looking there. - user2864740
I copied your code above into the same fiddle with no syntax errors. I'm using Chrome as well. Is there a way you can post your HTML code as well? - Ding
Ding - can you check the fiddle? it is not working... I get error by Firefox as welll. SyntaxError: expected expression, got '<' - Rayan Sp
Hi Ding, I found the issue, I just edited the post. How could I solve it? any idea? - Rayan Sp
It works fine for me:jsfiddle.net/o3das45q/2 - Rusty

2 Answers

1
votes

It looks like you are getting PHP errors which is adding certain characters to your script and giving you the expected expression, got '<' at < br. Looks like you need to fix your PHP errors first or switch off PHP error.

You could fix those errors Undefined variable: var by simply defining a initial value for those variables. i.e. $pagination= "";

However, I would recommend turning off PHP errors since it should only be enabled at development stage and those errors do not seem to be that critical.

0
votes

Thanks for user3284463 got it working by removing the $ signs from JS variables as the browser confused JS variables with PHP variables and skipped them because I was echoing JS.