I created a class dictionary for convenience only. You can use the keys of your object as a way to get the number associated with the day when flatpickr launches the onCreateDay callback. With the value associated with the day, you can get the class from the dictionary and, if it is not empty, add it to the element.
I added some clarification to the code to highlight some things that I find relevant.
You can check it in the script on this page (full screen if it is not displayed), or you can check it in this fiddle .
Hope this helps.
var dates = { 20161029: 3, 20161030: 0, 20161031: 0, 20161101: 4, 20161102: 4, 20161103: 4, 20161104: 5, 20161105: 1, 20161106: 0, 20161107: 4, 20161108: 3, 20161109: 3, 20161110: 4 }, classDict = { 0: 'redClass', 1: 'greenClass', 3: 'blueClass', 4: 'greyClass', 5: 'orangeClass' };
.redClass { background-color: red !important; } .greenClass { background-color: green !important; } .blueClass { background-color: blue !important; } .greyClass { background-color: grey !important; } .orangeClass { background-color: orange !important; }
<link rel="stylesheet" href="https://unpkg.com/flatpickr/dist/flatpickr.min.css"> <script src="https://unpkg.com/flatpickr"></script> <input id="dayCreate" type="text" placeholder="Select Date..">
UPDATE
The idea of โโthe dictionary was to simplify adding / removing classes and avoid ugly switches or long ifs. However, you can easily change the code to filter by value (only values โโgreater than 3 get classes), and add any class you want when this condition is met.
For example ( violin ):
function getClass(value) {
As you can see in the solutions I offer, there is no need to cycle through the object to get the value associated with the date, you can get it in constant time by compiling the date key from the date that flatpickr provides when building the day (onCreateDay callback).
UPDATE
According to the documentation (or so it seems), to get the date of the current day inside the onDayCreate callback, you should use the fp (currentYear and currentMonth) and dayElem (textContent) properties.
However, currentMonth always returns the month that the chart writer shows, not the month of the day (the calendar can show November, but the day can be in October or December), so you need to mess around a bit to avoid marking incorrect dates.
In this fiddle, you can find a solution that does not use dateObj, and works more as the documentation says.
And here is the code:
// Better always use a two digit format in your dates obj function get2DigitFmt(val) { return ('0' + val).slice(-2); } function getClass(value) { // Here you could put any logic you want. Ifs, add the value to a string, whatever... return value === 4 ? 'redClass' : 'blueClass'; } // Adjust month depending on the month day function getMonth(currentMonth, dayClass) { return currentMonth + (dayClass.contains('prevMonthDay') ? 0 : (1 + Number(dayClass.contains('nextMonthDay')))); } function getDateKey(year, month, day) { return year + get2DigitFmt(month) + get2DigitFmt(day); } // onDayCreate event, add class to day if date has a class flatpickr("#dayCreate", { onDayCreate: function(dObj, dStr, fp, dayElem) { var key = getDateKey(fp.currentYear, getMonth(fp.currentMonth, dayElem.className), dayElem.textContent), value = dates[key]; if (value > 3) { dayElem.className += ' ' + getClass(value); } } });