1

I was trying to highlight certain dates in a calendar using jQuery datepicker. I found the following code on this forum from Mark Murphy, Highlight dates in specific range with jQuery's datepicker which works perfectly, butt there are a couple of bits in the code that I don't understand. I can't post a comment on the original answer as I don't have enough privileges.

What I would like to know is:

1) what is the purpose of the || '' in the bit which returns the matching date (function returns true)?

2) what is the purpose of the , when the function returns false?

$(document).ready(function() {

var dates = {'2012/6/4':'some description' , '2012/6/6':'some other description'}; 

$('#datepicker').datepicker({                
   beforeShowDay: function(date) {

       var search = date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + (date.getDate());

       //console.log(search);

       if (dates[search]) {
           return [true, 'highlight', dates[search] || ''];
       }

       return [false, '', ''];
   }

});
Community
  • 1
  • 1
cpr
  • 33
  • 1
  • 5

1 Answers1

0

1) dates[search] || '' means that if dates[search] is not defined, it will return a void string

2) return [false, '', ''] return an array of 3 elements with values : false, void string, void string

The definition of the array to return from JQueryUI web site :

http://api.jqueryui.com/datepicker/#option-beforeShowDay

A function takes a date as a parameter and must return an array with [0] equal to true/false indicating whether or not this date is selectable, [1] equal to a CSS class name or "" for the default presentation, and [2] an optional popup tooltip for this date. It is called for each day in the datepicker before it is displayed.

sdespont
  • 13,915
  • 9
  • 56
  • 97
  • Ok, yes I had looked at the API link (I should have mentioned) but it doesn't mention about the option for returning something if the result is undefined - I assume that must be a standard JavaScript thing? I have searched on this forum and generally but haven't found any other examples of this. – cpr Dec 13 '12 at 09:54
  • Also is void string the same as empty string? (which is how I would describe ''). And for part 2) I'm not seeing 3 elements at all there, only 2. Could you please explain how "," equates to void string, void string? Sorry if all this is really obvious - I am quite new to JavaScript. – cpr Dec 13 '12 at 09:56
  • Tried to edit previous comment but not allowed - please ignore the question about there being 3 elements - I see this now - still getting used to the syntax! The question about whether void string is the same as empty string still remains... – cpr Dec 13 '12 at 10:04
  • You can't return `undefined`, return `return [true, '', ''];` if the date is selectable or `return [false, '', ''];` if not. And, of course, add special class (param 2) and tooltip info (param 3) if needed – sdespont Dec 13 '12 at 14:09
  • I'm just trying to understand the syntax in the return, where you've explained that whatever appears after the **||** is returned if **dates[search]** is actually not defined. I've not seen a return statement before which specifies what to do if something is undefined. Do you have a reference you could point me at? I'm still not clear why it's used in this case - if **dates[search]** is not defined then wouldn't the function return false anyway? – cpr Dec 13 '12 at 14:46