Solved

jQuery date picker default year selector when blank

Posted on 2010-11-26
6
1,100 Views
Last Modified: 2012-05-10
Hi,

We are using jquery.ui.datepicker to enter birth dates.
The average age of the birth dates in our database is 25 years old.

We bind the date picker like this;
$('#birthDate').datepicker({ yearRange: '1910:2005' }).change(calcAge);
Which is working well.

What we want is when the datepicker is clicked on and the current birth date is blank that the year selector defaults to 1985.
So the user dosn't have to scroll all the way from 1910
0
Comment
Question by:Matthew_Way
  • 3
  • 2
6 Comments
 
LVL 2

Expert Comment

by:marceloOrigoni
ID: 34217591
did you try this?



$('#birthDate').datepicker({ yearRange: '1910:2005' }).change(calcAge);
$('#birthDate').datepicker("setDate", '01/01/1985');

 Marcelo
0
 

Author Comment

by:Matthew_Way
ID: 34224293
That didn't seem to work.

Also I don't want to set the value in the field but just change the default list position in the year drop down.

0
 
LVL 6

Accepted Solution

by:
ventaur earned 500 total points
ID: 34233787
Try this:

$('#birthDate').datepicker({ defaultDate: '-25y' });

Open in new window


It should default the date picker to the current date minus your average age (25 years). It keeps things relative too so you don't have to remember to update it when your date range changes later.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:Matthew_Way
ID: 34246850
@ventaur
Great thanks so much, amazing just how many options are available under jQuery.
0
 
LVL 6

Expert Comment

by:ventaur
ID: 34248440
I think, perhaps, the author chose the wrong comment as the accepted solution.
0
 
LVL 6

Expert Comment

by:ventaur
ID: 34248445
You're welcome! Yes, there are a ton of options with jQuery. Too many to know them all.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now