• Hi,

    We are experiencing some really strange behaviour with a jQuery script.
    We are using the following script to toggle the sidebar (collapsed/not collapsed) on mobile devices.

    <script>
    jQuery(document).ready(function($){
    jQuery(‘#showfilter’).on(‘click’, function(){
    jQuery(‘#secondary’).toggleClass(‘show’);
    });
    )};
    </script>

    Notice the )}; at the end.
    It isn’t working with the correct syntax }); but when we change } and ) the script works fine.
    JS console is prompting the syntax error (Uncaught SyntaxError: Unexpected token ‘)’ ), but the script is still working.

    Any idea on why this is happening?

    You’ll have to look at the page with screen width smaller than 991px.

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Francesco Grasso

    (@francgrasso)

    YITH Support Representative

    Hi @joel2019,

    <script>
    jQuery(document).ready(function($){
    	$('#showfilter').on('click', function(){
    		$('#secondary').toggleClass('show');
    	});
    });
    </script>;

    can you please try this syntax? (note the jQuery closure not used in your previous script and the reversed brackets

    Thread Starter joel

    (@joel2019)

    Hi Francesco,

    Thanks for the swift response.
    I think you must have misread.

    I understand the syntax is correct the way you propose, but the script isn’t working when we use the correct syntax });

    It is working with the incorrect syntax )}; That’s what we find to be very strange.

    Furthermore:
    The ; at the end (after </script>) is not correct. Since the ; is outside the script tag, it will be rendered as html.

    Francesco Grasso

    (@francgrasso)

    YITH Support Representative

    yes the ; is typo from the copy and paste.

    Are you still having issues with the new syntax? I mean, by using the $ where possible.
    Adding the code in console, I cannot see issues…
    Also… where are you adding the code? maybe too early… I suggest to put it in the child theme and use wp_enqueue_script with the in_footer param

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘jQuery syntax’ is closed to new replies.