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…”

Share

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.

Share

4 Responses 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 [...]

  • Charlie d Says:

    This is really helpful….

    Are you able to provide example code ?

  • admin Says:

    I actually created a widget.

    I explain how to use it in this post: YUI: An Input Field Calendar Widget

    Demo is here: YUI Widgets

    Can be downloaded from Google Code: imAjaxWidgets

  • Charlie d Says:

    Thank you for this.

    It helps in a anyway, although what I am after is a solution to providing a calendar for multiple fields with field names built within a recordset loop, the recordset row total varies accounting to SQL criteria and therefore in some case there may only be 5 date fields and in other perhaps 50 or 60, as in example code ….

    <input type="text" id="” class=”DateField” name=”" value=”"size=”8″>

    I am guessing the solution is in your code var ids = ["tDate1", "tDate2"]; perhaps there is a way to dynamically grow the ID variables within some kind of array or way to build var id names in a loop within the javascript as many times as a recordset total row count value passed to the script.

    I don’t know really as I am no Javascript expert.

    Thank you for any help you can give in advance.

CommentLuv badge