Richard
Richard

Reputation: 91

How to add link (href) on selected days in jQuery Datepicker

I need small help with this script:

var disabledDays = ["2013-9-5", "2013-9-13", "2013-9-28"];
var tips = ['aaa', 'bbb', 'ccc'];
$("#datepicker").datepicker({
    showButtonPanel: false,
    firstDay: 1,
    dateFormat: 'yy-mm-dd',
    beforeShowDay: function (date) {
        var m = date.getMonth(),
            d = date.getDate(),
            y = date.getFullYear();
        for (i = 0; i < disabledDays.length; i++) {
            if ($.inArray(y + '-' + (m + 1) + '-' + d, disabledDays) != -1) {
                //return [false];
                return [true, 'highlight', tips[i]];
            }
        }
        return [true];
    }
});

http://www.jsfiddle.net/VPzL7/

I cant figure out how to use same method as i delect days to add href link on these selected - highlighted days.

Upvotes: 3

Views: 9214

Answers (3)

Richard
Richard

Reputation: 91

Try this solution:

HTML

<div id="datepicker"></div>

CSS

td.highlight {
    border: none !important;
    padding: 1px 0 1px 1px !important;
    background: none !important;
    overflow:hidden;
}
td.highlight a {
    background: #99dd73 url(bg.png) 50% 50% repeat-x !important;
    border: 1px #88a276 solid !important;
}

jQUery

var disabledDays = ["2013-9-20", "2013-9-24"];
var tips = ['some description1', 'some other description2'];
var hrefs = ['http://www.bbc.com/', 'http://www.cnn.com/'];

$("#datepicker").datepicker({
    showButtonPanel: false,
    firstDay: 1,
    dateFormat: 'yyyy-mm-dd',
    beforeShowDay: function (date) {
        var m = date.getMonth(),
            d = date.getDate(),
            y = date.getFullYear();
        for (var i = 0; i < disabledDays.length; i++) {
            if ($.inArray(y + '-' + (m + 1) + '-' + d, disabledDays) != -1) {
                return [true, 'highlight', tips[disabledDays.indexOf(y + '-' + (m + 1) + '-' + d)]];
            }
        }
        return [true];
    },
    onSelect: function(dateText, inst) {        
        var date = new Date(dateText.slice(4)),
            m = date.getMonth(),
            d = date.getDate(),
            y = date.getFullYear();        
        if ($.inArray(y + '-' + (m + 1) + '-' + d, disabledDays) != -1) {
            window.location = hrefs[disabledDays.indexOf((y + '-' + (m + 1) + '-' + d))];
        }
    }
});

Here is a working Fiddle. Hope it will be helpfull for others..

Upvotes: 6

redolent
redolent

Reputation: 4259

This isn't a direct answer to your question, but it can be a solution. If you need to redirect to a page, use the onSelect option (http://api.jqueryui.com/datepicker/#option-onSelect)

var links = { "2013-09-05": 'http://my-website.com/event/world-domination' };

//...

    onSelect: function (dateString) {
        if ( links[ dateString ] )
          document.location.href = links[ dateString ];
    }

Upvotes: 2

ffflabs
ffflabs

Reputation: 17481

It seems jQueryUI doesn't provide a way to do this, but you could assign each "special" date a second class to handle it afterwards

return [true, 'date_'+String(y)+String(m)+String(d)+' highlight', tips[i]];

Then you could add listeners to trigger when each special date gets clicked

jQuery(document).on('click','.date_201385',function() {
 do something;
});

Upvotes: 1

Related Questions