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?

1

3 Answers 3

4

The following code works:

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

(tested on the jQueryUI datepicker demo page)

Sign up to request clarification or add additional context in comments.

4 Comments

And if it's not working it may be related with incompatibilities?
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.
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.
Yeah I know and your answer is one of the codes I put so... thanks for your help anyway. I'll test them :D
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()
});

Comments

2

you can set date like this as well

$('.date-pick').datePicker().val(new Date()).trigger('change')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.