0

I'm trying to put 2 scripts in 1 js file and i'm getting:

SyntaxError: missing } after property list
note: { opened at line 9, column 19

But as far as I check all the curly brackets are closed, not sure what the real issue is.

Code

// country
jQuery( document ).ready( function( $ ) {
    $('select[name="country"]').on('change', function() {
      $.ajaxSetup({
          headers: { 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') }
      });
       var CountryId = $(this).val();
        if(CountryId) {
            $.ajax({
              url: '{{ url('getprovinces') }}/'+encodeURI(CountryId),
              type: "GET",
              dataType: "json",
              success:function(data) {
                $('select[name="province"]').empty();
                var options = data.map(function(state) {
                    return $("<option class='form-control'>").val(state.id)
                                        .text(state.name);
                });
                $('select[name="province"]').empty().append(options);
              }
            });
        }else{
          $('select[name="province"]').empty().append("<option class='form-control' value='' selected>Select</option>");
        }
    });
});

error comes from this line:

url: '{{ url('getprovinces') }}/'+encodeURI(CountryId),

Any idea?

Shidersz
  • 16,846
  • 2
  • 23
  • 48
mafortis
  • 6,750
  • 23
  • 130
  • 288

3 Answers3

1

You need to escape the quotes or use a double-quoted string. Otherwise JS thinks the string ends on the quote after url(' then gets confused when a variable name pops up.

kingdaro
  • 11,528
  • 3
  • 34
  • 38
0

Try like that:

// country
jQuery( document ).ready( function( $ ) {
    $('select[name="country"]').on('change', function() {
      $.ajaxSetup({
          headers: { 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') }
      });
       var CountryId = $(this).val();
        if(CountryId) {
            $.ajax({
              url: '{{' + url('getprovinces') + '}}/' + encodeURI(CountryId),
              type: "GET",
              dataType: "json",
              success:function(data) {
                $('select[name="province"]').empty();
                var options = data.map(function(state) {
                    return $("<option class='form-control'>").val(state.id)
                                        .text(state.name);
                });
                $('select[name="province"]').empty().append(options);
              }
            });
        }else{
          $('select[name="province"]').empty().append("<option class='form-control' value='' selected>Select</option>");
        }
    });
});
PPShein
  • 13,309
  • 42
  • 142
  • 227
0

Try

url: url('getprovinces') + '/' + encodeURI(CountryId),
holydragon
  • 6,158
  • 6
  • 39
  • 62