FrozenHeart
FrozenHeart

Reputation: 20746

How to populate datepicker's input element with its defaultDate

Is there any way to populate datepicker's input element with its defaultDate?

HTML

<input type="text" id="datepicker">

JS

$("#datepicker").datepicker({
    defaultDate: +7
});

https://jsfiddle.net/k9fdw2ub/

I want users to see the default date in the input element itself, not just in the pop-up calendar.

Upvotes: 0

Views: 41

Answers (3)

priya_singh
priya_singh

Reputation: 2488

Jsfiddle

Try below code

$("#datepicker").datepicker({

    dateFormat: 'dd/mm/yy',
});

$('#datepicker').datepicker('setDate', 'today + 7');

Upvotes: 2

kernal_lora
kernal_lora

Reputation: 1155

I think this will work for you.

$("#datepicker").datepicker({
    defaultDate: +7
});

 $('#datepicker').datepicker('setDate', 'today');
// $('#datepicker').datepicker('setDate', 7); If you wanna set date from future 7days from current date use this

Upvotes: 1

Charantej Golla
Charantej Golla

Reputation: 598

By using set setDate you can achieve it.

$("#datepicker").datepicker({ dateFormat: "yy-mm-dd"}).datepicker("setDate", new Date());
<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>


<input type="text" id="datepicker">

Upvotes: 1

Related Questions