Lee
Lee

Reputation: 1280

Jquery Datepicker, at wits end

This code used to work but now it doesn't and it's only taking the first value in the array...

var unavailableDates is an array that stops dates showing on the datepicker..

any ideas??

It's not cycling through all dates in the array for some reason!?!?

var unavailableDates = ["4-7-2011","5-7-2011"];

function unavailable(date) {
    var dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();
    if ($.inArray(dmy, unavailableDates) == 0) {
        return [false, "", "Unavailable"];
    } else {
        var day = date.getDay();
        return [(day != 0 && day != 2 && day != 3 && day != 4 && day != 6)];
    }
}

$(function(){

    $('#smh').datepicker({
        showOn: "both",
        buttonImage: "images/calendar.gif",
        buttonImageOnly: true,
        beforeShowDay: unavailable,
        minDate: -0,
        dateFormat: "dd/mm/yy",
        onSelect: function(e) {
        e = e.split('/')[1] + '/' + e.split('/')[0] + '/' + e.split('/')[2];
        var date = new Date(e);
        var day = date.getDay(); // 0 = sunday etc...        
        if (day === 1) {
            $("#check2").hide();
            $("#text").hide();
            $("#check1").show();

        } else if (day === 5) {
            $("#check1").hide();
            $("#text").hide();
            $("#check2").show();

        } 
        $("#bdate").html(this.value);
    } 
    })

Upvotes: 0

Views: 161

Answers (2)

Lourens
Lourens

Reputation: 1518

What happens is $.inArray returns the index of the found item(if it finds it, else -1)

so youll need to check if the index is not -1

...$.inArray(dmy, unavailableDates) != -1...

Upvotes: 0

Luc125
Luc125

Reputation: 5857

In jQuery the $.inArray(elem, array) method returns -1, and not 0, when elem is not found in array.

So I think you should use:

if ($.inArray(dmy , unavailableDates) == -1)

instead of:

if ($.inArray(dmy, unavailableDates) == 0)

Upvotes: 3

Related Questions