I am getting correct result(disabling particular week) when passing the array statically,But not working while passing dynamic array.I am reading on string from PHP and converting that into array and passing to date picker.If have checked the dynamic array is array or not and that values also..It is giving if ($.inArray(day, daysArray) != -1)
is -1 always.I have checked daysArray.length
value also it is 3.
It is working for daysToDisable
array and not working for daysArray
array.Both arrays values are same.
Can any body please help me. I did not understand Why It is not working.
$(function () {
var days="<?php echo $newstr; ?>";
var daysArray=days.split(' ');alert(daysArray); // not working for this array.Here the out of this array is [2,4,5] same as below array.
var daysToDisable = [2, 4, 5]; //Working for this value.
$('#txtDate').datepicker({
beforeShowDay: disableSpecificWeekDays
});
function disableSpecificWeekDays(date) {
var day = date.getDay();
for (i = 0; i < daysToDisable.length; i++) {
if ($.inArray(day, daysToDisable) != -1) {
return [false];
}
}
return [true];
}
})
i think i found the issue
after var daysArray=days.split(' ');
your daysArray will have array data like ["1","2","3"]
but we need data to be in [1,2,3]
.
Your final code can look like
$(function () {
var days="<?php echo $newstr; ?>";
var daysArray=days.split(' ');alert(daysArray); // not working for this array.Here the out of this array is [2,4,5] same as below array.
for (day in daysArray ) {
daysArray[day] = parseInt(daysArray[day], 10);
}
//var daysToDisable = [2, 4, 5]; //Working for this value.
$('#txtDate').datepicker({
beforeShowDay: disableSpecificWeekDays
});
function disableSpecificWeekDays(date) {
var day = date.getDay();
for (i = 0; i < daysArray.length; i++) {
if ($.inArray(day, daysArray) != -1) {
return [false];
}
}
return [true];
}
})
it is happening because on converting the php string, it is generating
["2", "4", "5"]
not [2, 4, 5]
Try this
var daysToDisable = <?php echo json_encode(explode(' ',$newstr), JSON_NUMERIC_CHECK); ?>;
instead of
var days="<?php echo $newstr; ?>";
var daysToDisable=days.split(' ');alert(daysArray);