Home > front end >  javascript ajax Uncaught SyntaxError: missing ) after argument list
javascript ajax Uncaught SyntaxError: missing ) after argument list

Time:07-04

when i use ajax to bring information at this line it tells me Uncaught SyntaxError: missing ) after argument list at this line

$.each('data.color',function(key,value){
      $('select[name="color"]').append('<option value=" 'value' ">" 'value' "</option>');
});

please help

here is my code javascript ajax

<script>
$(document).ready(function(){
  $('body').on('click','#addcart',function(){
    var id = $(this).val();
    // console.log(id);
    $.ajax({
      type: 'GET',
      url: '/product/view/modal/' id,
      dataType: 'json',
      success:function(data){
        // console.log(data);
        $('#pname').text(data.product.name_en);
        $('#price').text(data.product.selling_price);
        $('#pcode').text(data.product.code);
        $('#pcategory').text(data.product.category.name_en);
        $('#pbrand').text(data.product.brand.brand_name_en);
        $('#pimage').attr('src','images/' data.product.thumbnail);
        $.each('data.color',function(key,value){
          $('select[name="color"]').append('<option value=" 'value' ">" 'value' "</option>');
        });
      }
    })        
  });
});
</script>

CodePudding user response:

You can fix the argument either way:

append(`<option value="${value}">"${value}"</option>`)

or

append('<option value="'   value   '">"'   value  '"</option>')

CodePudding user response:

The error is here:

.append('<option value=" 'value' ">" 'value' "</option>');
                          ^
                          |
                        ERROR

Javascript detects that you are trying to start a new statement but have not finished the previous statement. Therefore it checks what was the last statement and notices that you have not closed the .append() function. So it is telling you that the ) is missing.

Of course, you do not intend to start a new statement but rather continue with an expression. What you want is this:

.append('<option value=" '   value   ' ">" '   value   ' "</option>');

        \_________________________ ______________________________/
                                  V
                Now this whole part is an expression
  • Related