change this line:
$('#retrieveDate').text($.datepicker.formatDate('yy-dd-mm', myDate));
by this: to add text by jQuery you should use val()
$('#retrieveDate').val($.datepicker.formatDate('yy-dd-mm', myDate));
change this line:
$('#retrieveDate').text($.datepicker.formatDate('yy-dd-mm', myDate));
by this: to add text by jQuery you should use val()
$('#retrieveDate').val($.datepicker.formatDate('yy-dd-mm', myDate));
Maybe the problem is your declaration of ‘myDate’ as var and then trying to write it as a text. Try converting myDate to a String after you add 15 days.
I picked this code from jQuery forum.
$(function () {
$("#selectedDate").datepicker({
dateFormat: 'yy-mm-dd',
onSelect: function(_date) {
var myDate = $(this).datepicker('getDate');
console.log(myDate);
myDate.setDate(myDate.getDate() + 15);
$('#retrieveDate').text($.datepicker.formatDate('yy-dd-mm', myDate));
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<div class="form-group">
<label>Selected Date in Datepicker</label>
<input type="text" name="selected_date" class="form-control datepicker"
id="selectedDate">
</div>
<div class="form-group">
<label>Retrieve Date</label>
<input type="text" id="retrieveDate" name="retrieve_date"
class="form-control">
</div>
When I select the date in first input, the second input must be automatically filled with the date that is 15 days more from the selected date in the datepicker.
Inputs usually have values, can you try changing
just use .val(..) instead of .text(…) .val is used for inputfields. text is used for non inputfields
jQuery UI’s Datepicker component documentation shows it has a setDate
function for this purpose
Thanks It worked. I had tried using .val() but don’t why it didn’t work and now it is working. Thank you all.
It looks like there are 2 date formats