Can't Code
Can't Code

Reputation: 59

Dynamic disable of day Date Picker JavaScript

I want to disable days dynamically by using array,

replacing the code :

var monday = 1;
var tuesday = 2;

with a dynamic value from a database

 var disabledDay = ["1","2","3"];

Any help will really be appreciated Thanks

old code

jsFiddle: http://jsfiddle.net/314wd8t7/

 $("#picker").datepicker(
    { beforeShowDay: function(day) {
  
    var string = jQuery.datepicker.formatDate('yy-mm-dd', day);
    var day = day.getDay();
    var monday = 1;
    var tuesday = 2;

    if (day != monday && day != tuesday){
      return [ true ]
    } else {
      return [ false ]
    }  
     }
});


$('#picker').datepicker();

<div id="picker"></div>

Upvotes: 0

Views: 707

Answers (1)

Brenden
Brenden

Reputation: 2097

I think you want something like this.

http://jsfiddle.net/dgo48jry/

const disabledDays = ["1", "2", "3"]
    .map((n) => parseInt(n))
    .filter((n) => !isNaN(n));

$("#picker").datepicker({
    beforeShowDay: function(day) {
        return [!disabledDays.includes(day.getDay())];
    }
});


$('#picker').datepicker();

This assumes that your server is returning the values as strings. If you dont need string, you can simply remove the map filter lines.

Upvotes: 1

Related Questions