Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

datepicker Error = Uncaught TypeError: Cannot read property '0' of undefined

Can't get jQueryUI datepicker option beforeShowDay to work.

I've tried different help topics I've found but I can't get it working.

I get this error

Uncaught TypeError: Cannot read property '0' of undefined

This is my JS. but it doesn't seems to work

<script type="text/javascript">
jQuery(document).ready(function () {
    var your_dates = [new Date(13, 5, 5), new Date(13, 5, 10)];
    jQuery("div#event-calender").datepicker({
        beforeShowDay: function (date) {
            if (jQuery.inArray(date.toString(), your_dates) != -1) {
                return [true, 'highlight'];
            }
        }
    });
});
</script>
like image 366
user2407193 Avatar asked May 22 '13 17:05

user2407193


1 Answers

You need to always return an array, not just when a condition matches.

From the documentation:

beforeShowDay

Type: Function( Date date )

A function that takes a date as a parameter and must return an array

jQuery(document).ready(function () {
    var your_dates = [new Date(13, 5, 5), new Date(13, 5, 10)];
    jQuery("div#event-calender").datepicker({
        beforeShowDay: function (date) {
            var arr;
            if (jQuery.inArray(date.toString(), your_dates) != -1) {
                arr = [true, 'highlight'];
            }else {
                arr = [true, ''];
            }
            return arr;
        }
    });
});

FIDDLE

like image 70
adeneo Avatar answered Nov 15 '22 06:11

adeneo