5

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!

2 Answers 2

5

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')
Sign up to request clarification or add additional context in comments.

Comments

4

You have problems with quotes. You may either escape \' the quotes around radio or simply change the code as follows:

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

2 Comments

It worked! But I also had to change this: $("input[type='radio']").click(function() {
@user1749898 Yes, both selectors had the same problem.

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.