TopicObserver.com

Trond Pettersen on Web Development and Topic Maps

Month: February, 2009

Forcing jQuery UI Datepicker to Always Open on Today’s Date

Today, I implemented the jQuery UI Datepicker in an application where the requirements state that the current date should always be null. That is, if the user opens a Datepicker after first having selected or typed in a date, the Datepicker should still be “centered” around today’s date with no pre-selected date.

It might just be me, but I was not able to find a way to do this by simply using the Datepicker options. I therefore ended up solving it by creating an invisible text field to which the given Datepicker is associated. In addition, an onClose callback function ensures that the value of this field is always re-set to null while the selected date is moved into the field to be read on the server side.

Using the same kind of HTML structure as listed in Replacing the Built-In DatePicker in Sun IdM 8.0, I ended up with JavaScript similar to the code listed below.

$(document).ready( function() {
	/**
	 * If the user actively selected a date, insert that date into
	 * the text field and reset the datepicker's value as this should
	 * always display the current date (CIMS req.).
	 */
	var updateDatePicker = function(date, datepicker) {
		if( date != null ) {
			var $dateHolderElm = datepicker.input;
			var dateFieldId = $dateHolderElm.attr('dateFieldId');
			$('#' + dateFieldId).val(date.format(dateFormat.masks.isoDate));
			$dateHolderElm.val('');
		}
	};
	$('div.datepicker.enabled').each( function() {
		// Create a new element that will act as the datepicker
		// for this field. Because we want the datepicker to always
		// open on the current day instead of the selected date,
		// we bind it to an invisible input element through a new
		// button element's onclick event handler.
		var dateFieldId    = $('.date', this).attr('id');
		var dateHolderId   = dateFieldId + "_dateholder";
		var $dateHolderElm = $('<input type="text" id="' + dateHolderId + '" value="" />');
		$dateHolderElm.attr('dateFieldId', dateFieldId);
		var $buttonElm = $('<button><img src="images/calendar.png" alt="Open calendar" /></button>');
		$buttonElm.insertAfter($('.date', this));
		// Make the dateholder invisible, but do not set display to none
		$dateHolderElm.css( {
			//visibility: 'hidden', // breaks in IE, so .. the rest is based on site design, but should be part of CSS
			border: 0,
			color:  'white',
			backgroundColor: 'white',
			zIndex: -1,
			position: 'relative',
			marginLeft: '-3px',
			marginTop: '-10px',
			height: 0,
			width:  0
		});
		$($dateHolderElm).insertBefore($buttonElm); // to ensure placement of dp
		$buttonElm.click( function() {
			$dateHolderElm.trigger('focus');
			return false;
		});
		$dateHolderElm.datepicker({
			onClose : updateDatePicker
		});
	});
});

Note: for formatting of JavaScript dates, I’ve used date.format.js from stevenlevithan.com.

Time to End IE6 … ?

You have probably already heard about the Norwegian campaign to end IE6. If not, see Ajaxian or Wired.

Although I sure hope that we (and in particular, me :D ) will spend less time on making web sites work perfectly in IE6 during 2009 than previous years, I don’t think we’ve seen the last of this dreadful browser. Read the full post »

Topic Maps 2009

Maybe I’ve been sleeping through classes, but I had absolutely no idea that the registration for Topic Maps 2009 in March had opened.

SOS Barnebyer: A Hint of Fresh Air (even on the Web)

Nowadays I seldom complain to web site owners about browser (or rather, standards) compability issues, mainly because the standard answer seemed to be “Sorry, but we can’t support ever browser out there.”.

So when I contacted SOS Barnebyer about a web form that had no submit button when viewed in Firefox on Ubuntu 8.04 a couple of days, I didn’t really expect an answer.

However, after a single working day, I received my answer:

Thank you for contacting us. The error has now been fixed.

No, thank you :)

  • RSS @twitter

    • Could not connect to Twitter
  • Tags

  • Topics

  • Recent Comments

  • Topic Map Feeds