Completing a Simple Date Picker with JavaScript and CSS - Finishing up
(Page 4 of 4 )
Just before we finish up, we can do one more thing to tidy up the functionality of the datepicker. There is currently no way to close the date picker without picking a date, so if it is opened accidentally after a date has already been picked, the visitor has to go through the process of selecting the date again. A little extra code in the showPicker() function can ensure that the datepicker can be switched off easily in this situation:
function showPicker() {
if (pickeropen == "no") {
document.getElementById("picker").style.display = "block";
pickeropen = "yes";
} else {
document.getElementById("picker").style.display = "none";
pickeropen = "no";
}
}
You'll need to add a global variable of var pickeropen = "no"; somewhere in the file. I usually find it best to add all of the global variables at the top of the file. That's it; you now have one fully fledged datepicker that saves typing for your visitors and makes form processing easier for you. You can also add pickeropen = "no"; to the end of the hidePicker() function as well.
Don't forget that you'll still need to check all of the data that's being submitted in the form for security reasons, but this is something that you'd need to do anyway. I just want to make it clear that just because you know exactly which format your dates are being submitted in, doesn't mean you don't need to guard against malicious user input.
Some fully fledged date picker applications allow the visitor to select the day of the week as well as the day, month and year aspects of the date. While this approach has obvious benefits in the corporate world, allowing members of staff at a company to select date ranges that they would like to book off work as vacation time by selecting Monday to Friday of a specific week or month instead of having to enter each date sequentially for example, it does create a lot more coding. When this information is not required, the extra code and complexity can be done away with; the date picker shown in this example is easy to code and compact while remaining usable and effective. It also works consistently across IE 6 and 7 and FireFox 1 and 2.0, so most if not all of your visitors should be able to use it without any problems.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |