JavaScript
  Home arrow JavaScript arrow Page 2 - Refining a Simple Date Picker with JavaScr...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Refining a Simple Date Picker with JavaScript and CSS
By: Dan Wellman
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 11
    2007-03-07

    Table of Contents:
  • Refining a Simple Date Picker with JavaScript and CSS
  • Getting the day element
  • Refining the code
  • More refinements

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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");

    More JavaScript Articles
    More By Dan Wellman


       · This part of the tutorial looks at some of the JavaScript to make the calender div...
     

    JAVASCRIPT ARTICLES

    - More on JavaScript Array Objects
    - Methods of the DOM Location Object
    - The DOM Location Object Properties
    - Handling Remote Files with JavaScript Click ...
    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...


     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT