YUI Library Home

Site Search (YDN & YUIBlog):

# YUI Library Examples: Button Control: Simple Calendar Menu Button

# Button Control: Simple Calendar Menu Button

This example demonstrates how to create a Menu Button whose Menu instance displays a Calendar.

View example in new window.

Date Month: Day: Year:

Begin by defining an anonymous function in order to keep all variables out of the global scope. Inside the anonymous function, define some shortcuts to utils that will be used frequently (Dom and Event).

(function () { var Event = YAHOO.util.Event, Dom = YAHOO.util.Dom; }());

Inside the the anonymous function, use the `onDOMReady` method of the Event utility to instantiate an Overlay and a Button when the page's DOM is ready to be scripted.

Event.onDOMReady(function () { var oCalendarMenu; // Create an Overlay instance to house the Calendar instance oCalendarMenu = new YAHOO.widget.Overlay("calendarmenu", { visible: false }); // Create a Button instance of type "menu" var oButton = new YAHOO.widget.Button({ type: "menu", id: "calendarpicker", label: "Choose A Date", menu: oCalendarMenu, container: "datefields" }); });

Once the new Button is created, add a listener for its "appendTo" event that will be used to render its Overlay instance into the same DOM element specified as the containing element for the Button.

oButton.on("appendTo", function () { // Create an empty body element for the Overlay instance in order // to reserve space to render the Calendar instance into. oCalendarMenu.setBody(" "); oCalendarMenu.body.id = "calendarcontainer"; });

Add a listener for the Button's "click" event that will be used to create a new Calendar instance. (Defering the creation and rendering of the Calendar until the firing of the "click" event improves the intial load time of the Button instance.)

var onButtonClick = function () { // Create a Calendar instance and render it into the body // element of the Overlay. var oCalendar = new YAHOO.widget.Calendar("buttoncalendar", oCalendarMenu.body.id); oCalendar.render(); // Subscribe to the Calendar instance's "select" event to // update the month, day, year form fields when the user // selects a date. oCalendar.selectEvent.subscribe(function (p_sType, p_aArgs) { var aDate; if (p_aArgs) { aDate = p_aArgs[0][0]; Dom.get("month-field").value = aDate[1]; Dom.get("day-field").value = aDate[2]; Dom.get("year-field").value = aDate[0]; } oCalendarMenu.hide(); }); // Pressing the Esc key will hide the Calendar Menu and send focus back to // its parent Button Event.on(oCalendarMenu.element, "keydown", function (p_oEvent) { if (Event.getCharCode(p_oEvent) === 27) { oCalendarMenu.hide(); this.focus(); } }, null, this); var focusDay = function () { var oCalendarTBody = Dom.get("buttoncalendar").tBodies[0], aElements = oCalendarTBody.getElementsByTagName("a"), oAnchor; if (aElements.length > 0) { Dom.batch(aElements, function (element) { if (Dom.hasClass(element.parentNode, "today")) { oAnchor = element; } }); if (!oAnchor) { oAnchor = aElements[0]; } // Focus the anchor element using a timer since Calendar will try // to set focus to its next button by default YAHOO.lang.later(0, oAnchor, function () { try { oAnchor.focus(); } catch(e) {} }); } }; // Set focus to either the current day, or first day of the month in // the Calendar when it is made visible or the month changes oCalendarMenu.subscribe("show", focusDay); oCalendar.renderEvent.subscribe(focusDay, oCalendar, true); // Give the Calendar an initial focus focusDay.call(oCalendar); // Re-align the CalendarMenu to the Button to ensure that it is in the correct // position when it is initial made visible oCalendarMenu.align(); // Unsubscribe from the "click" event so that this code is // only executed once this.unsubscribe("click", onButtonClick); }; /* Add a "click" event listener that will render the Overlay, and instantiate the Calendar the first time the Button instance is clicked. */ oButton.on("click", onButtonClick);

Lastly, style the Button with a calendar icon.

#calendarpicker button { background: url(../button/assets/calendar_icon.gif) center center no-repeat; text-align: left; text-indent: -10em; overflow: hidden; *margin-left: 10em; /* For IE */ *padding: 0 3em; /* For IE */ white-space: nowrap; }

## Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

### YUI Logger Output:

Note: You are viewing this example in debug mode with logging enabled. This can significantly slow performance.

Reload with logging
and debugging disabled.

### Button Control Examples:

- Push Buttons
- Link Buttons
- Checkbox Buttons
- Radio Buttons
- Submit Buttons
- Reset Buttons
- Menu Buttons
- Split Buttons
- Simple Calendar Menu Button
- Calendar Menu Button with Date on Button Face
- Color Picker Button
- Fixed Width Menu Button
- Glowing Button
- Slider Button
- Using the Button ARIA Plugin
- Using A Menu Button To Replace A <select> Element
- Replacing the content of a Button's Menu
- Combobox, with CSS for Multiple Stacked Instances (included with examples for AutoComplete Control)
- Showing, Hiding, and Reordering Columns. (included with examples for DataTable Control)
- Getting CSS Style Sheets (included with examples for Get Utility)
- Complex Application (included with examples for Layout Manager)

### More Button Control Resources:

- User's Guide (external)
- API Documentation
- Cheat Sheet PDF (external)

Copyright © 2010 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings