Home arrow JavaScript arrow Page 2 - Refining a Simple Date Picker with JavaScript and CSS
JAVASCRIPT

Refining a Simple Date Picker with JavaScript and CSS


In part one of this series, we looked at the HTML and CSS needed to construct the date-picker popup and style it accordingly. In this part, we are going to write the JavaScript that is going to make it work. This is going to be accomplished with a mixture of plain JavaScript with some DOM retrieval and manipulation.

Author Info:
By: Dan Wellman
Rating: 4 stars4 stars4 stars4 stars4 stars / 13
March 07, 2007
TABLE OF CONTENTS:
  1. · Refining a Simple Date Picker with JavaScript and CSS
  2. · Getting the day element
  3. · Refining the code
  4. · More refinements

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Refining a Simple Date Picker with JavaScript and CSS - Getting the day element
(Page 2 of 4 )

For this, we first need to go back to the HTML document and add some additional code. The function that captures the day that has been selected will rely on a parameter being passed to it by the element that makes the function call. For each day element in the HTML file, add the following code, changing the parameter for each day:

onclick="getDay(1)"

While you have the HTML file open, you need to make a couple of other minor changes. First, to stop the web page causing the parent folder to open when a day in the datepicker is selected, just add the # symbol to all of the href attributes of the day links. Second, to make the page valid, you'll need to change the ID attributes of the three days that have an ID (29, 30 and 31). Add the letter D to the beginning of each one, making them D29, D30 and D31 respectively.

The function back in the JavaScript file that captures the day that was selected simply sets the day sent as a parameter to the day variable declared at the top of the file:

function getDay(Day) {
  
day = Day;
}

I mentioned before that the function handling the day elements would be a little more complicated than the other two functions. The reason for this is that as things stand now, a visitor could select the month of February and the day of the 31st. Such a date does not exist, however, so we should therefore ensure that it cannot be selected. We also need to factor in leap years.

First of all, we can make it so that the month and the year should be selected first. If the visitor selects a day first, we can let them know that they should choose the month and year first. Add the code in bold below to the getDay() function:

function getDay(Day) {
  
if (month == "" || year == "") {
    
alert("Please choose the Month and Year first");
    
return false;
  
} else {
    
day = Day;
  
}
}

The code checks whether either of the two variables are still empty, and alerts the visitor if so. If both the variables contain data, the script proceeds to set the variable with the visitor's choice. This alone however won't take the days that don't exist out of the date picker. To do this, we need to add some more code to the getMonth() function.

Below the second line of the function add the following if statement:

if (month == "April" || month == "June" || month == "September" || month == "November") {
  
var unday = document.getElementById("31");
  
unday.className = "dateunlive";
  
unday.removeChild(unday.childNodes[0]);
  
unday.removeAttribute("href");
  
unday.removeAttribute("onclick");

So we check whether the month variable matches any of the months that have 30 days, and if so, get the element with an ID of 31 in a variable called unday. We then set the class to dateunlive and remove the text node (the number in the box) followed by the href and onclick attributes which changes the block from yellow to gray. As you can see, I've left out February. More days need to be altered with February so the else if statement that catches this month is also longer, but essentially the same as the above code:

} else if (month == "February") {
  
var unday1 = document.getElementById("D31");
  
var unday2 = document.getElementById("D30");
  
var unday3 = document.getElementById("D29");
  
unday1.className = "dateunlive";
  
unday1.removeChild(unday1.childNodes[0]);
  
unday1.removeAttribute("href");
  
unday1.removeAttribute("onclick");
  
unday2.className = "dateunlive";
  
unday2.removeChild(unday2.childNodes[0]);
  
unday2.removeAttribute("href");
  
unday2.removeAttribute("onclick");
  
unday3.className = "dateunlive";
  
unday3.removeChild(unday3.childNodes[0]);
  
unday3.removeAttribute("href");
  
unday3.removeAttribute("onclick");


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials