Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery: Uncaught SyntaxError: Unexpected identifier

My website cannot show the prices for different product options and I found the following error message on Chrome inspection:

Uncaught SyntaxError: Unexpected identifier

It is pointing to the line below:

  var first_variant_price = $('ul li input[type='radio']:checked').attr('data-price'); 

This is the complete script:

<script type='text/javascript' charset='utf-8'>
$(document).ready(function(){ 
  var first_variant_price = $('ul li input[type='radio']:checked').attr('data-price'); 
  $('.current-price').text( first_variant_price );
  $('input[type='radio']').click(function() {
    var variant_price = $(this).attr('data-price');
    $('.current-price').text( variant_price);
  });
});
</script>

I could not realize any mismatch on this code. Thanks for the help!

like image 570
Valladão Avatar asked Oct 16 '12 11:10

Valladão


People also ask

How do I fix uncaught SyntaxError unexpected identifier?

To solve the "Uncaught SyntaxError: Unexpected identifier" error, make sure you don't have any misspelled keywords, e.g. Let or Function instead of let and function , and correct any typos related to a missing or an extra comma, colon, parenthesis, quote or bracket.

What is unexpected end of input in Javascript?

The "Uncaught SyntaxError Unexpected end of input" error occurs for 3 main reasons: Forgetting a closing parenthesis, bracket or quote. Trying to parse an empty response with JSON.


1 Answers

If you use quotes inside other quotes, either use a different type or escape them:

$("ul li input[type='radio']:checked") // different type of quotes

or

$('ul li input[type=\'radio\']:checked') // escape inner quotes via backslash

but the easiest way is to not quote the radio at all - it's not necessary.

$('ul li input[type=radio]:checked')
like image 157
Christoph Avatar answered Sep 30 '22 03:09

Christoph