Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Birthday Picker dealing with form refresh

I'm using an excellent jquery plugin for Birthday Date picking for my web form.

Demo here: http://abecoffman.com/stuff/birthdaypicker/

The problem I'm dealing with is on a form validation. My form validation redirect back to the same page and I'll lose the date that was picked.

Is there any way to add an option to the javascript of the date picker: http://abecoffman.com/stuff/birthdaypicker/bday-picker.js

so that I can set a "default selected date". The config can work to something like:

$("#picker1").birthdaypicker({
  chosenDay: 1;"
  chosenMonth: 28;"
  chosenYear: 2011;"
 });

This will set the "selected" date to January 28, 2011.

like image 224
Yada Avatar asked Jan 28 '11 16:01

Yada


1 Answers

Without modifying the plugin you could use JQuery to set the values. The markup that the plugin generates looks like this:

<fieldset class='birthday-picker'>
    <select class='birth-year' name='birth[year]'></select>
    <select class='birth-month' name='birth[month]'></select>
    <select class='birth-day' name='birth[day]'></select>
</fieldset>

So your JQuery would simply have to get a hold on those elements and set their value. You'd want to make sure you're setting a value that exists in the select list.

$('select.birth-year').val('my_year');      // 1980
$('select.birth-month').val('my_month');    // 1-12 (not "Jan" which is the text)
$('select.birth-day').val('my_day');        // 1-31 (keep month limitation in mind)

This code would have to be called after the plugin has been called, otherwise the markup won't have been generated yet.

like image 70
Abe Avatar answered Oct 12 '22 23:10

Abe