JavaScript
  Home arrow JavaScript arrow 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 
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 / 13
    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


    (Page 1 of 4 )

    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.

    All you'll need for this article is the source code from the first article and a simple text editor. In this text editor, add the first of the functions that we need: the function that allows the date picker to be opened. As the calendar has been hidden from display initially with CSS, we can adjust this rule dynamically using the DOM to alter the required element:

    function showPicker() {
      
    document.getElementById("picker").style.display = "block";
    }

    Don't forget to go back to the datepicker.htm file and add id="picker" to the picker div, and an onclick="showPicker();" call to the calendar image. You'll also need to link the newly created JavaScript file to the HTML file with something along the lines of

    <script type="text/javascript" src="datepicker.js">
    </script>

    Next we need to get the date selected by the visitor and put it in the form field. There are essentially three elements to capture so we can start off by defining three empty variables:

    var day = "";
    var month = "";
    var year = "";

    We'll skip the function that gets the day for now and move on to the function that gets the month selected by the visitor:

    function getMonth() {
      
    var option = document.getElementById("month").selectedIndex;
      
    month = document.getElementById("month").options[option].text;
    }

    First we get the option that has been chosen in the select box, and then we get the value of the text inside the selected option. The function that gets the selected year is equally as simple:

    function getYear() {
      
    var option = document.getElementById("year").selectedIndex;
      
    month = document.getElementById("year").options[option].text;
    }

    Everything is the same except the id of the select box. You'll also need to go back to the HTML file and add the required ID and onchange attributes to the two select objects. Now we can go back to getting the selected day element.

    More JavaScript Articles
    More By Dan Wellman


       · This part of the tutorial looks at some of the JavaScript to make the calender div...
       · Sorry to say but i'm a bit disappointed with the quality of this tutorial. It looks...
       · Well, I'm sorry you feel that way, unfortunately, most people don't like to read...
       · Can you post the working version of the date picker first - where can i download it...
       · To be honest, I can't post anything here, the articles are generated via a CMS. If...
       · To the Author:I am just a little confused on the second page of part 2 of this...
       · Doesn't anyone put demos up anymore?
     

    JAVASCRIPT ARTICLES

    - Google Ajax APIs: Building a Navigation Bar ...
    - Easing jQuery Plug-in: Using the easeOutBack...
    - jQuery Easing Plug-in: Building a Navigation...
    - Easing jQuery Plug-In: Fixing Access Issues ...
    - Easing jQuery Plug-in: Implementing the Beha...
    - jQuery Easing Plug-in: Building an Animated ...
    - Dynamic Drop-down Menus: Javascript Progress...
    - Creating Drop-down Menus Using Progressive E...
    - Building Slide Shows Using Progressive Enhan...
    - Form Validation with Progressive Enhancement...
    - Validating Web Forms: an Introduction to Pro...
    - Ajax Progressive Enhancement: Reading Remote...
    - Ajax Image Requests: Progresive Enhancement ...
    - jQuery Image Gallery: Working with Progressi...
    - Progressive Enhancement in an Image Gallery







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek