Ajax and Ajax Frameworks

I have been a developer for nearly 20 years and a web developer for about 10 years. Over the years, I have considered creating a blog to share the things I have learned (and am learning) about web design and development, but I never seemed to have the time. When I began learning about web development using Ajax and Ajax Frameworks, I decided to take the time to create this blog.

My intention for this blog is to focus primarily on Ajax Frameworks, but since web development requires knowledge of many technologies, I will occasionally write about things such as CSS, Javascript, PHP, MySQL, Flash, etc.

Lately, I have been using JQuery as my primary Ajax tool. Although I have used other Ajax Frameworks in the past (Dojo Toolkit, Yahoo! User Interface Library, Scriptaculous/Prototype), JQuery has thus far been the easiest to learn. In my first few posts I will discuss some of the JQuery Plugins that I have created. Some of these include: a plugin for Google Maps (jquery.imGoogleMaps), Form validation and submission  (jquery.imValidateForm), Page Populater (jquery.imPagePopulate), and a plugin to create lists (jquery.imList).

While I am still learning about some of the other Ajax Frameworks, I hope that what I have learned will be helpful to others. Just remember, “When you can pull the pebbles from my hand…”

YUI: Using Event.addListener. One Calendar, Multiple Fields

Friday, April 10th, 2009

I am working on a YUI application where I have multiple date fields. The date fields are regular input fields. When a user clicks on the input field a YUI Calendar appears. When a date is selected from the calendar, the date value is inserted into the appropriate input field.

To set up multiple input fields to use one calendar, I first created a variable and set it to null:

?View Code JAVASCRIPT
var clickEl = null;

I then used an Event Listener (Event.Listener) using the ids of the input fields:

?View Code JAVASCRIPT
var ids = ["appoint_date", "interp_date", "service_date", "cancel_date"];
Event.addListener(ids, "click", showCal);

When a user clicks on one of the input fields, the showCal function is called. The showCal function sets the clickEl variable to the id of the “clicked” input field and then shows a YUI Dialog component that contains the YUI Calendar.

?View Code JAVASCRIPT
function showCal(e) {
     clickEl = this.id;
     dlgCal.show();
}

When a date is selected, the date value is inserted into the appropriate input field:

?View Code JAVASCRIPT
...
var selDate = calCal.getSelectedDates()[0];
var dStr = selDate.getDate();
var mStr = calCal.cfg.getProperty("MONTHS_SHORT")[selDate.getMonth()];
var yStr = selDate.getFullYear();
Dom.get(clickEl).value = yStr + "-"+ mStr + "-" + dStr;
...

Of course the alternative is to create a separate calendar for each input field. Too much work (and obviously not necessary). Enjoy.

Related posts

One Response to “YUI: Using Event.addListener. One Calendar, Multiple Fields”

  • YUI: An Input Field Calendar Widget | GrasshopperPebbles.com Says:


    [...] I first began the project, I created an event listener (Event.Listener) as discussed in a previous post.  But, because a date field is fairly common in web applications,  I wanted to create a widget so [...]

CommentLuv Enabled