Chirantan Chirantan - 1 month ago 6
jQuery Question

Jquery Datepicker not selecting the default date

I have my datepicker set up like this

$("#scheduled_date_163").datepicker({
onSelect: function(dateText, inst) {
jQuery.get("/tasks/inplace_edit?id=scheduled_date_163&class_name=Task&value=" + dateText, function(data){
$('#scheduled_date_163').html(data);
$('#scheduled_date_163').removeClass('hasDatepicker');
})
}
});

$('#scheduled_date_163').datepicker( "option", "defaultDate", $.datepicker.parseDate("d m y", "31 8 2009") );


As you can see the date is hardcoded. It is supposed to be replaced with some code. However, the datepicker refuses to pick the date up! It always shows me the current date. Unable to figure out why. I even tried giving the date in string and
+7
etc. But today's date itself is shown as the default date.

What could be causing this?

UPDATE

The element with scheduled_date_163 is not a textfield. Its a div.On click of which the above written datepicker javascript executes.

<div id="scheduled_date_163">
<p class='date'>None</p>
</div>


There is not way that the div ID repeats on the page.

Answer

DEMO: http://jsbin.com/uqevo3/2/edit

$("#scheduled_date_163").datepicker({
   defaultDate: "31-8-2009", //OR $.datepicker.parseDate("dd mm yy", "31 8 2009")
   dateFormat: 'dd-mm-yy', // format as ##-##-####
    onSelect: function(dateText, inst) {
      jQuery.get("/../" + dateText,  function(data){
        $('#scheduled_date_163').html(data);
        $('#scheduled_date_163').removeClass('hasDatepicker');
      })
    }
  });

NOTE: no needs to declare two times the same datepicker, this is your main problem!

then if you need to set some constant options for multiple datepickers, you may want to use the Utility functions .setDefaults()