%calAttrs; ]> jQuery Calendar

jQuery Calendar Compatibility Mode

Defaults

The calendar can easily be added to an input field with appropriate default settings.

$('#defaultFocus').calendar();

Default calendar (click to show):

IE select issue:

The defaults are:

Processed fields are marked with a class of hasCalendar and are not re-processed if targetted a second time.

Invocation

The calendar can be invoked in a variety of ways, as shown below. Also shown is the use of an image only trigger, and how the controls and buttons appear when disabled.

Calendar appears on focus:

Calendar appears via button:

Calendar appears both ways:

This is achieved by overriding settings when attaching the calendar to the appropriate input controls. Note that you can also override the default settings that apply to all calendar instances.

popUpCal.setDefaults({autoPopUp: 'both', buttonImageOnly: true,
	buttonImage: 'calendar.gif', buttonText: 'Calendar'});
$('#invokeFocus').calendar({autoPopUp: 'focus', yearRange: '-5:+5'});
$('#invokeButton').calendar({autoPopUp: 'button', buttonImageOnly: false,
	buttonImage: '', buttonText: '...', yearRange: '-7:+7'});
$('.invokeBoth').calendar();

The calendar can also be triggered externally for a particular input.

External trigger:

popUpCal.showFor($('#invokeFocus')[0]);

Or even opened as a "dialog". If the blockUI plugin is available, it is used to create a modal dialog.

Calendar dialog:

popUpCal.dialogCalendar($('#invokeDialog').val(), setDateFromDialog,
{prompt: 'Choose a date', speed: ''});

Keystrokes

The calendar also responds to keystrokes entered in the input field.

Keyboard driven:

The relevant keystrokes are:

Restricting

You can restrict the functionality of the calendar in various ways. The first example sets the first day of the week to Monday and prevents it from being changed, as well as preventing the month and year from being selected directly.

Restricted functionality:

$('#restrictControls').calendar({firstDay: 1, changeFirstDay: false,
changeMonth: false, changeYear: false});

You can also limit the range of dates selectable within the calendar. Here it's between 26-Jan-05 and 26-Jan-07.

Limited dates:

Note that the range of selectable months and years changes appropriately. Also, note that the Today link is no longer available as today is not in the range. By default, the Prev and Next links are disabled if they are not applicable. You can override this to remove them instead, with the hideIfNoPrevNext setting.

$('#restrictDates').calendar({minDate: new Date(2005, 1 - 1, 26),
maxDate: new Date(2007, 1 - 1, 26)});

Customise

You can customise the selectability and/or appearance of individual days by setting a callback function that accepts a date and returns an array with the first entry being true/false for selectability and the second entry being a CSS class to apply (or '' for none). One appropriate function is built-in that prevents the selection of days on weekends.

No weekends:

$('#noWeekends').calendar({customDate: popUpCal.noWeekends});

Or you can provide your own function. The one below highlights and prevents selection of a set of national days.

National days:

$('#nationalDays').calendar({customDate: nationalDays});

var natDays = [[1, 26, 'au'], [2, 6, 'nz'], [3, 17, 'ie'], [4, 27, 'za'], 
[5, 25, 'ar'], [6, 6, 'se'], [7, 4, 'us'], [8, 17, 'id'], [9, 7, 'br'], 
[10, 1, 'cn'], [11, 22, 'lb'], [12, 12, 'ke']];
function nationalDays(date) {
	for (i = 0; i < natDays.length; i++) {
		if (date.getMonth() == natDays[i][0] - 1 && date.getDate() == natDays[i][1]) {
			return [false, natDays[i][2] + '_day'];
		}
	}
	return [true, ''];
}

With CSS like the following:

.au_day { color: blue !important; background: #eee url(au.gif) no-repeat center !important; }

Localisation

You can localise the calendar for other languages and regional differences. The calendar defaults to English with a date format of DD/MM/YYYY. This is easily changed via the dateFormat setting. The first three characters are 'Y', 'M', and 'D' in the order in which they should appear, and the last character is the field separator to use (may be omitted).

ISO date format:

$('#isoFormat').calendar({dateFormat: 'YMD-'});

Or translate the text into another language.

:

You need to load the appropriate language package, which adds a language set (popUpCal.regional[langCode]) and automatically sets this language as the default for all calendars.

<script type="text/javascript" src="jquery-calendar-fr.js"></script>

Thereafter, if desired, you can restore the original language settings.

popUpCal.setDefaults(popUpCal.regional['']);

And then configure the language per calendar instance.

$('#l10nCalendar').calendar(popUpCal.regional['fr']);

Localisation packages:

Localisation packages can be found below under Settings and Documentation.

Date Range

Use a custom field settings function to create a date range control: two date fields, each restricting the other. The function takes an input field as an argument and returns a settings object (anonymous).

Date range: to

$('.calendarRange').calendar({fieldSettings: customRange});
		
function customRange(input) {
	return {minDate: (input.id == 'dTo' ? getDate($('#dFrom').val()) : null),
		maxDate: (input.id == 'dFrom' ? getDate($('#dTo').val()) : null)};
}

Select a date range on one calendar, first click is the start date and second click is the end date. In the last example, Previous and Next move three months at a time.

Date range:

$('#rangeSelect').calendar({rangeSelect: true});

Show two months with date range select:

$("#rangeSelect2Months").calendar({rangeSelect: true, numberOfMonths: 2);

Show six months with date range select:

$("#rangeSelect6Months").calendar({rangeSelect: true, numberOfMonths: [2, 3],
	stepMonths: 3, prevText: '<< Previous Months', nextText: 'Next Months >>'});

Miscellaneous

Set the date shown when first opening the calendar. The default is today.

Open at 01-Jan-2007:

Open at 7 days from today:

$('#openDateJan01').calendar({defaultDate: new Date(2007, 1 - 1, 1)});
$('#openDatePlus7').calendar({defaultDate: +7});

Additional settings let you move the Clear/Close controls to the bottom and display the days in other months (non-selectable). A callback function is also added to operate when a date is selected. If no callback is specified, the onchange event of the input field is triggered.

Additional settings:

$('#addSettings').calendar({closeAtTop: false,
	showOtherMonths: true, onSelect: alertDate});

Connect the calendar to linked drop-downs. You still need an input field, but it is hidden following the drop-downs.

Linked drop-downs:

$('#linkedCalendar').calendar({minDate: new Date(2001, 1 - 1, 1),
	maxDate: new Date(2010, 12 - 1, 31), fieldSettings: readLinked,
	onSelect: updateLinked});

function readLinked() {
	$('#linkedCalendar').val($('#selectDay').val() + '/' +
		$('#selectMonth').val() + '/' + $('#selectYear').val());
	return {defaultDate: new Date($('#selectYear').val(),
		$('#selectMonth').val() - 1, $('#selectDay').val())};
}

function updateLinked(date) {
	$('#selectDay').val(date.substring(0, 2));
	$('#selectMonth').val(date.substring(3, 5));
	$('#selectYear').val(date.substring(6, 10));
}

Reconfiguration

If necessary, the calendar for an input (or set of inputs) can be reconfigured on the fly. As an example, here we change the speed at which the calendar appears.

Display speed:

Reconfigured calendar:

When the option in the select changes, the following function is called:

function setSpeed(select) {
	popUpCal.reconfigureFor('#reconfigureCal',
		{speed: select.options[select.selectedIndex].value});
}

Calendars can also be configured inline. Add attributes to the input control with the namespace "cal:" corresponding to the calendar properties. The attribute values are evaluated as JavaScript.

Inline configuration 1: blank

Inline configuration 2:

<input type="text" size="10" class="inlineConfig" onchange="showDay(this);"
	cal:closeAtTop="false" cal:firstDay="1" cal:appendText="which is a"/>

<input type="text" size="10" class="inlineConfig"
	cal:minDate="new Date(2007, 1 - 1, 1)" cal:maxDate="new Date(2007, 12 - 1, 31)"/>

$('.inlineConfig').calendar();

Inline

The calendar can be used inline rather than as a popup.

Inline date range: to

Selected range is:

Connect the calendar to a div or span rather than an input field. Include a callback function for notification of a date selection.

$('.calendarInline').calendar({onSelect: updateInlineRange});
			
function updateInlineRange() {
	var inlineFrom = $('#inlineFrom')[0];
	var inlineTo = $('#inlineTo')[0];
	var dateFrom = popUpCal.getDateFor(inlineFrom);
	var dateTo = popUpCal.getDateFor(inlineTo);
	$('#inlineRange').val(formatDate(dateFrom) + ' to ' + formatDate(dateTo));
	popUpCal.reconfigureFor(inlineFrom, {maxDate: dateTo});
	popUpCal.reconfigureFor(inlineTo, {minDate: dateFrom});
}

Or show a number of months at once and set the range directly.

Inline date range with two months:

Selected range is:

$('#rangeInline').calendar({rangeSelect: true, rangeSeparator: ' to ',
	numberOfMonths: 2, onSelect: updateInlineRange2});
	
function updateInlineRange2(dateStr) {
	$('#inlineRange2').val(dateStr ? dateStr :
		formatDate(popUpCal.getDateFor('#rangeInline')));
}

Stylesheets

The calendar can be formatted through the use of a stylesheet. The default stylesheet is used in the rest of this demo with an alternate one used here.

Alternate style:

In a dialog:

The basic HTML structure of the calendar is shown below:

<div id="calendar_div">
  <div class="calendar_control">
    <div class="calendar_clear"><a>Clear</a></div>
    <div class="calendar_close"><a>Close</a></div>
  </div>
  <div class="calendar_links">
    <div class="calendar_prev"><a>&lt;Prev</a></div>
    <div class="calendar_current"><a>Today</a></div>
    <div class="calendar_next"><a>Next&gt;</a></div>
  </div>
  <div class="calendar_oneMonth calendar_newRow">
    <div class="calendar_header">
      <select class="calendar_newMonth"></select>
      <select class="calendar_newYear"></select>
    </div>
    <table class="calendar" cellpadding="0" cellspacing="0">
      <thead>
        <tr class="calendar_titleRow">
          <td><a>Su</a></td><td><a>Mo</a></td>...
        </tr>
      </thead>
      <tbody>
        <tr class="calendar_daysRow">
          <td class="calendar_daysCell calendar_weekEndCell
            calendar_otherMonth calendar_unselectable">31</td>
          <td class="calendar_daysCell"><a>1</a></td>
          <td class="calendar_daysCell calendar_daysCellOver"><a>2</a></td>
          <td class="calendar_daysCell calendar_daysCellOver calendar_currentDay"><a>3</a></td>
          <td class="calendar_daysCell calendar_today"><a>4</a></td>
          ...
        </tr>
        ...
      </tbody>
    </table>
  </div>
  <div class="calendar_oneMonth">
    ...
  </div>
  <div style="clear: both;"></div>
</div>