Home arrow Style Sheets arrow Page 3 - Creating a Simple Date Picker with JavaScript and CSS
STYLE SHEETS

Creating a Simple Date Picker with JavaScript and CSS


Getting visitors to fill out certain parts of forms can be tricky, especially when the item you want them to fill out can be rendered in many different formats. Consider how many different ways one can write a date, for instance. In this first part of a two-part article, you will learn how to build a date picker that minimizes this headache.

Author Info:
By: Dan Wellman
Rating: 4 stars4 stars4 stars4 stars4 stars / 58
February 21, 2007
TABLE OF CONTENTS:
  1. · Creating a Simple Date Picker with JavaScript and CSS
  2. · An Example Form
  3. · The Calendar Popup
  4. · The CSS

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Creating a Simple Date Picker with JavaScript and CSS - The Calendar Popup
(Page 3 of 4 )

Next we have to code the calendar popup which allows your visitors to select the required date. To do this, we can just create a floating div that sits above the page but remains unseen until the user hits the calendar icon. In the HTML file, begin the following div:

<div class="picker">
  <select class="month">
   
<option>Month</option>
   
<option>January</option>
   
<option>February</option>
   
<option>March</option>
   
<option>April</option>
   
<option>May</option>
   
<option>June</option>
   
<option>July</option>
    
<option>August</option>
   
<option>September</option>
   
<option>October</option>
   
<option>November</option>
   
<option>December</option>
 
</select>

  <select class="year">
   
<option>Year</option>
   
<option>2000</option>
   
<option>2001</option>
   
<option>2002</option>
   
<option>2003</option>
   
<option>2004</option>
   
<option>2005</option>
   
<option>2006</option>
   
<option>2007</option>
   
<option>2008</option>
   
<option>2009</option>
 
</select>

All we've done so far is opened the container div and added two select box elements containing months and years. I've added the class attributes required for the CSS to work in this section so that we don't have to keep going back to the HTML file.

The scope of the years select box is going to differ depending on the date you are asking your visitors to enter. A date of birth request, for example, is going to have a far greater range of dates than a delivery date choice. In this example I've used a simple range to illustrate the functionality.

Select elements, while difficult and inconsistently styled across browsers, seem the best choice here in terms of minimizing and simplifying the code required. Another way we could do it would be to use a normal text input field, with a graphic representing a button placed directly at its right-hand edge, and an invisible div below it containing multiple paragraph elements that hold the choices. When the graphic button is selected, the div, aligned directly with the bottom of the text field, becomes visible. When one of the p elements is selected, DOM scripting methods could be used to copy its contents to the text field. It could be done in this way, but this is outside of the scope of this article and would result in a great deal more code. Now let's move on to the days part of the date-picker:

  <a href="" class="datelive">1</a><a href=""
class="datelive">2</a><a href="" class="datelive">3</a><a href=""
class="datelive">4</a><a href="" class="datelive">5</a><a href=""
class="datelive">6</a>
  <a href="" class="datelive">7</a><a href=""
class="datelive">8</a><a href="" class="datelive">9</a><a href=""
class="datelive">10</a><a href="" class="datelive">11</a><a
href="" class="datelive">12</a>
  <a href="" class="datelive">13</a><a href=""
class="datelive">14</a><a href="" class="datelive">15</a><a
href="" class="datelive">16</a><a href="" class="datelive">17</a><a href="" class="datelive">18</a>
  <a href="" class="datelive">19</a><a href=""
class="datelive">20</a><a href="" class="datelive">21</a><a
href="" class="datelive">22</a><a href="" class="datelive">23</a><a href="" class="datelive">24</a>
 
<a href="" class="datelive">25</a><a href=""
class="datelive">26</a><a href="" class="datelive">27</a><a href="" class="datelive">28</a><a href=""
class="datelive">29</a><a href="" class="datelive">30</a>
 
<a href="" class="datelive">31</a><a href=""
class="dateunlive"></a><a href="" class="dateunlive"></a><a
href="" class="dateunlive"></a><a href="" class="dateunlive"></a>

</div>

The days part is simply a long list of link elements which will be styled to represent squares, with each square corresponding to a different day. We also close the div off because as far as the HTML is concerned, that is all we need. I agree that it's not pretty to look at (either code-wise or on the page), but when we come to the CSS for it, everything can be straightened out.


blog comments powered by Disqus
STYLE SHEETS ARTICLES

- CSS Combinators: Working with Child Combinat...
- CSS Combinators: Using General Siblings
- Intro to CSS Combinators
- CSS Semicircles and Web Page Headers
- Drawing Circular Shapes with CSS3 and Border...
- More CSS Pagination Link Templates
- CSS Pagination Links
- Animated CSS3 Image Gallery: Advanced Transi...
- CSS3 Animated Image Gallery: Transitions
- CSS3 Properties: Fixed Heights with box-sizi...
- CSS3 Properties: Altering Strokes and 3D Eff...
- CSS3 Properties: Text-Stroke
- CSS3 Transitions: Width and Height Properties
- Creating a Drop Down Menu in CSS3
- Intro to CSS Transitions

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 5 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials