Datepicker is a wonderful option provided with the user interface to facilitate users to select a date in the correct format. In this article, we are going to add a date picker to a web page, using the jQuery plugin.
jQuery Datepicker plugin is provided with various options and methods, that are used to customize the date picker user interface and the calendar widget.
Let us have an example, to see how to add this plugin to provide the date picker user interface to a web page. With this example, first, we are going to display the calendar in a default manner without using any customization with the help of the options and methods provided.
For adding a jQuery date picker, we need to go with the following steps.
<html>
<head>
<title>JQuery DatePicker</title>
<link rel="stylesheet"
href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script>
$(function() {
$( "#txtDate" ).datepicker();
});
</script>
</head>
<body>
<div>
Select Date: <input type="text" id="txtDate" />
</div>
</body>
</html>
In the above code, we have added the required Javascript libraries as the source of accessing jQuery built-in functions. For that, we can refer to these libraries either by using a direct URL that requires the internet or we can make a local copy of these files by downloading the latest version from jQuery’s official website.
After that, the date picker() is invoked with the reference of the HTML input element id. So, by default, the input element is the target, where the selected date will be displayed, and also, used to trigger event to popup calendar widget.
If we want to have two elements separately, for triggering the date picker event and for populating the response, we need to go with the available jQuery date picker plugin options and methods.
There are several options and methods provided by the jQuery Datepicker plugin. For example, the following list contains some of the jQuery plugin options that are used to customize date picker settings.
All options including those that are not specified in the above list, are used to apply changes with respect to a specific selector.
Let us have an example to work with date picker some of the options from the above list.
<html>
<head>
<title>JQuery DatePicker</title>
<link rel="stylesheet"
href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script>
$( "#txtDate" ).datepicker({
showOn: "button",
buttonImage: "datepicker_icon.png",
buttonText: "Date Picker",
buttonImageOnly: true});
});
</script>
</head>
<body>
<div>
Select Date: <input type="text" id="txtDate" />
</div>
</body>
</html>
If you don’t have such an image as specified in the above script, then, the value of the buttonText option will be shown to the browser.
Datepicker methods are used to perform various functions including some familiar date functions, such as date getters and setters. Some of these functions are,
Apart from the above list of a function, this plugin also includes some utility functions for performing operations like clearing default date picker settings, formatting dates and so on.
For example, setDefaults() function is used to clear the default setting by specifying required options and values. And the code is,
<html>
<head>
<title>JQuery DatePicker</title>
<link rel="stylesheet"
href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script>
$.datepicker.setDefaults({
showOn: "button",
buttonImage: "datepicker_icon.png",
buttonText: "Date Picker",
buttonImageOnly: true
});
$(function() {
$( "#txtFromDate" ).datepicker();
});
</script>
</head>
<body>
<div>
From Date: <input type="text" id="txtFromDate" />
</div>
</div>
</body>
</html>
Using this setDeafults() function, we can apply options once for all date picker instances available on our website.
Download jQuery Datepicker Source Code
Thanks for this jquery datepicker tutorial. Really simple and helpful.