1

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/

7
  • A syntax error is a syntax error. It should also include a line/character number. Start by looking there. Commented Mar 20, 2016 at 4:51
  • 1
    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? Commented Mar 20, 2016 at 4:58
  • Ding - can you check the fiddle? it is not working... I get error by Firefox as welll. SyntaxError: expected expression, got '<' Commented Mar 20, 2016 at 5:00
  • Hi Ding, I found the issue, I just edited the post. How could I solve it? any idea? Commented Mar 20, 2016 at 5:44
  • It works fine for me:jsfiddle.net/o3das45q/2 Commented Mar 20, 2016 at 6:31

2 Answers 2

1

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.

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

7 Comments

Thanks for your reply. I turned them off but still no luck. any other suggestion? can you check if the right jQuery library loaded on my website? gulf-brokers.com
It looks like those errors are still popping up. Take a look at this:stackoverflow.com/questions/1308379/…
I added error_reporting(0); @ini_set('display_errors', 0); to wp-config the error changed on firefox but still the same on chrome.
Did you try adding define('WP_DEBUG_DISPLAY', false); at the top of wp-config.php
@RayanSp I'm using firefox and I too see the errors
|
0

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.

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.