How to resolve Uncaught SyntaxError: Unexpected token ‘==’ in JQuery-UI datepicker

You can’t use an inline if to conditionally set object properties in that way.

Here’s a few options:

Pre-assign options:

var options = {
      dateFormat: 'dd-mm-yy',
      changeMonth: true,
      changeYear: true,
      showAnim: 'slideDown',
      duration: 'fast',
      minDate: +1,
      setDate: new Date(),
     yearRange: new Date().getFullYear() + ':' + new Date().getFullYear(),
};
if (weekendval == 0) {
     options.beforeShowDay = $.datepicker.noWeekends,
}
$('.commencement_date').datepicker(options).datepicker('setDate', '1');

Use Object.assign (or jquery.extend if you need to support ancient browsers):

$('.resumption_date').datepicker(Object.assign({
      dateFormat: 'dd-mm-yy',
      changeMonth: true,
      changeYear: true,
      showAnim: 'slideDown',
      duration: 'fast',
      minDate: +1,
      yearRange: new Date().getFullYear() + ':' + new Date().getFullYear(),
      enableOnReadonly: true,
      beforeShow: function(i) {
        if ($(i).attr('readonly')) {
          return false;
        }
      }
    }, weekendval == 0 ? { beforeShowDay: $.datepicker.noWeekends } : {})
).datepicker('setDate', '1');

If using ES6 syntax this can also be written as:

$('.resumption_date').datepicker({
      dateFormat: 'dd-mm-yy',
      changeMonth: true,
      changeYear: true,
      showAnim: 'slideDown',
      duration: 'fast',
      minDate: +1,
      yearRange: new Date().getFullYear() + ':' + new Date().getFullYear(),
      enableOnReadonly: true,
      beforeShow: function(i) {
        if ($(i).attr('readonly')) {
          return false;
        }
      },
      ...(weekendval == 0 ? { beforeShowDay: $.datepicker.noWeekends } : {}))
    }, 
).datepicker('setDate', '1');

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top