Learner
Learner

Reputation: 786

How to manipulate jquery datepicker by passing array of dates?

my requirements is as follows;

If date of the datepicker matches the passed dates using array, then it should show hello on hover over and else show sold. I tried the following code

var rateArray = [new Date(2014, 1, 1), new Date(2014, 1, 2)]; // just some dates.
var date = new Date();
$(document).ready(function () {

    $("#test").datepicker({
        beforeShowDay: function (date) {
            if ($.inArray(date, rateArray)) {
                return [true, 'tiptip', 'hello'];
            }
            else {
                return [false, 'tiptip', 'Sold'];
            }
        }


    });

});

But it is not working, it is showing "hello" on every date. If anybody know the solution then please help, thanks.

Upvotes: 0

Views: 1205

Answers (2)

mplungjan
mplungjan

Reputation: 177955

Inspired by a now deleted answer:

Live Demo

var dates = {};
dates[new Date('02/13/2014').getTime()]='Holiday1';
dates[new Date('02/14/2014').getTime()]='Holiday2';
dates[new Date('02/15/2014').getTime()]='Holiday3';

$('#txtDate').datepicker({
    beforeShowDay: function(date) {
      var hlText = dates[date.getTime()];
      return (hlText)?[true, "tiptip", hlText]: [true, '', ''];
    }
});

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

Try

//use a string representation for the date
var rateArray = ['01 Jan 2014', '02 Jan 2014']; // just some dates.
$(document).ready(function () {
    $("#test").datepicker({
        beforeShowDay: function (date) {
            //convert the date to a string format same as the one used in the array
            var string = $.datepicker.formatDate('dd M yy', date)
            // $.inArray() returns -1 if the item is not found so change the condition accordingly
            if ($.inArray(string, rateArray) > -1) {
                return [true, 'tiptip', 'hello'];
            } else {
                return [false, 'tiptip', 'Sold'];
            }
        }
    });
});

Demo: Fiddle

Upvotes: 1

Related Questions