1

I'm having some issues with datepicker in jQuery.

I'm trying to put the today date in a field which is a datepicker.

I've tried with:

$('#fecha_alta').datepicker('setDate', new Date());
console.log($('#fecha_alta').datepicker('getDate'));

And

$('#fecha_alta').datepicker('setDate', '+0');
console.log($('#fecha_alta').datepicker('getDate'));

But always log null and also the input shows empty.

What's wrong?

Arnaud Leymet
  • 5,995
  • 4
  • 35
  • 51
Antonio Laguna
  • 8,973
  • 7
  • 36
  • 72
  • check out http://stackoverflow.com/questions/606463/jquery-datepicker-set-selected-date-on-the-fly – minaz Sep 01 '11 at 16:25

3 Answers3

4

The following code works:

$(document).ready(function() {
  $('#datepicker').datepicker('setDate', new Date());
});

(tested on the jQueryUI datepicker demo page)

Arnaud Leymet
  • 5,995
  • 4
  • 35
  • 51
  • And if it's not working it may be related with incompatibilities? – Antonio Laguna Sep 01 '11 at 17:11
  • 1
    The easiest way for you to tackle this issue is to create a jsfiddle.net space with the most minimalist code for your to reproduce this issue. If the issue persists, you'll be able to share with us your problem and can expect a quick analysis from us folks. – Arnaud Leymet Sep 01 '11 at 17:14
  • And of course, when I'm using '#datepicker' you should use '#fecha_alta' since it's the ID you use. My answer was generic and can be applied to the jQueryUI demo page. – Arnaud Leymet Sep 01 '11 at 17:15
  • Yeah I know and your answer is one of the codes I put so... thanks for your help anyway. I'll test them :D – Antonio Laguna Sep 01 '11 at 17:18
3

If you need to set the date to the current one by default, then you may want to set it at start, when creating the widget:

$('#datepicker').datepick({
  defaultDate: new Date()
});
Arnaud Leymet
  • 5,995
  • 4
  • 35
  • 51
2

you can set date like this as well

$('.date-pick').datePicker().val(new Date()).trigger('change')
Gayan
  • 2,750
  • 5
  • 47
  • 88