Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS - Uncaught SyntaxError: Unexpected Token

Good morning guys,

I decided to start learning JavaScript this weekend and am now stuck at the first hurdle. I have tried googling / researching the reason for this error and, as far as I can see, everything is formatted correctly. Are you able to help? The code is designed to make an arrow shaped button, made from 2 small divs in a container, on my webpage glow when hovered over.

$(document).ready(function() {
var $toggleButton = $('.toggle-button'),
    $bartop = $('.toggle-button.menu-bar-top'),
    $barbottom = $('.toggle-button.menu-bar-bottom'),


$toggleButton.hover(function() {
    $bartop.css("box-shadow", "0 0 10px 1px #fff");
    $barbottom.css("box-shadow", "0 0 10px 1px #fff");
}, function() {
    $bartop.css("box-shadow", "0");
    $barbottom.css("box-shadow", "0");
});
});

I have added "box-shadow: 0;" to the .toggle-button .menu-bar-top and .toggle-button .menu-bar-bottom css classes, but I suspect it is something to do with my JS code. I am working in Chrome v55. Any help with this would be greatly appreciated!

like image 668
Richard Goodman Avatar asked Nov 29 '25 20:11

Richard Goodman


2 Answers

You have a , at the end of line 4. So the Uncaught SyntaxError: Unexpected token . error is due to $toggleButton.hover.

For your browser you are still declaring a new variable but you can use . in variable names.

To fix your problem just replace the , by a ; in line 4:

$(document).ready(function() {
  var $toggleButton = $('.toggle-button'),
      $bartop = $('.toggle-button.menu-bar-top'),
      $barbottom = $('.toggle-button.menu-bar-bottom');


  $toggleButton.hover(function() {
    $bartop.css("box-shadow", "0 0 10px 1px #fff");
    $barbottom.css("box-shadow", "0 0 10px 1px #fff");
  }, function() {
    $bartop.css("box-shadow", "0");
    $barbottom.css("box-shadow", "0");
  });
});
like image 54
Jspdown Avatar answered Dec 02 '25 03:12

Jspdown


It's giving you syntax error as you have used comma at the end in following statement

$barbottom = $('.toggle-button.menu-bar-bottom'),

Because of the comma, javascript is considering $toggleButton as variable declaration as it's on the next line. And it is already declared at the top, so javascript is throwing syntax error, that it is already declared. Remove comma and use semicolon ;.

like image 35
prasad Avatar answered Dec 02 '25 03:12

prasad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!