cwg
cwg

Reputation: 87

Syntax error code - Java script

Normally My code is working fine. but, I get Syntax error. what is mistake in syntax here?

enter image description here

$(document).ready(function() {
$('.myClass').change(function() {
        var ids = ['first', 'second'];
        var totalCount = ids.reduce((prev, id) => parseInt($(`#${id}-passenger`).val()) + prev, 0);
        var mc = $('input[name="myClass"]:checked  + label').text();
        $('#myTotal').val(totalCount + ' - '+mc);
    });
});

Upvotes: -1

Views: 95

Answers (2)

Rory McCrossan
Rory McCrossan

Reputation: 337701

Your code is entirely valid and works fine. Therefore I would surmise that this issue is simply that the JS linter in the IDE you're using is outdated and doesn't support ES6. I'd suggest using a more up to date linter, assuming the IDE lets you change it, or even a better IDE entirely.

If you want to avoid the issue you would need to remove the template literals and arrow functions, like this:

$('.myClass').change(function() {
  var ids = ['first', 'second'];
  var totalCount = ids.reduce(function(prev, id) {
    return parseInt($('#' + id + '-passenger').val()) + prev, 10);
  }, 0);
  var mc = $('input[name="myClass"]:checked + label').text();
  $('#myTotal').val(totalCount + ' - ' + mc);
});

Upvotes: 1

Sparw
Sparw

Reputation: 2743

Maybe you have a Javascript version issue and you should try without templating ?

var totalCount = ids.reduce(function (prev, id) {
    return parseInt($('#' + id + '-passenger').val()) + prev
}, 0);

Hope it helps.

Upvotes: 4

Related Questions