How to create a simple datepicker using jQuery

jQuery allows you to easily create a datepicker and customize it according to your requirements. Here's a quick look at how you can get started with using the jQuery datepicker.

The jQuery website has a nice application called Themeroller that lets you customize the interface for jQuery UI widgets. There are also plenty of themes available in the gallery that you can download and use directly. Copy the stylesheet, images, the jQuery file and the jQuery-UI files into the appropriate directories and link them in the header of your HTML file.

Now let's create a simple form that contains the text field that you want to convert into a datepicker.

<form action="index" method="get">
  <input id="date-pick" name="date-pick" type="text" value="" />
  <input id="date-submit" name="date-submit" type="submit" value="Go!" />
</form>

To convert the input field with the id date-pick into a datepicker, write the following jQuery code in the head section of your HTML.

$(function(){
  $("#date-pick").datepicker();
});


That's it! The input field that you created now works as a popup datepicker. A calendar showing the current month appears whenever you focus on the #date-pick field and the date you click on appears as text in the field.

There are tons of options to customize the datepicker, such as changing the date format sent to the server or using a different language for the calendar. You can explore these options in the jQuery UI documentation for the datepicker.

2 comments:

Shantharam Shenoy K said...

Hey Nithin, it worked fine..but i have one little doubt, i could not re-size it. I mean its width is too large and is causing problems. I tried tinkering around in the css but it did not work. Can you help me here.
Thanks in advance

Nithin said...

@Shantharam. You could try this in the css to resize the datepicker. Put it after you include the jquery-ui stylesheet.

div.ui-datepicker {
  font-size: 62.5%;
}

Post a Comment