0

I need to fire event while default date is selected at the time of page load using Jquery datepicker. I am explaining my code below.

<input name="date" type="text" id="alltour" class="datepickerdate" placeholder="" value="" >

$('#alltour').datepicker({
       dateFormat: 'dd-mm-yy',
       minDate: 0,
       beforeShowDay: enableAllTheseDays,
       onSelect: function(dateText, inst) {
            $('#alltour').val(dateText);
            console.log('selected date',$('#alltour').val());
       }
}).datepicker("setDate", new Date());

Here when page is loading the default date is selected and when this default date will populate in the field a event should be fired. As per my code its now working for first time default date selected.

2 Answers 2

0

There is bug that has been raised to jquery on this context https://bugs.jqueryui.com/ticket/8066

However there is a workaround suggested in the question https://stackoverflow.com/a/13693779/5866606 for this by just clicking the current day manually with jquery.

$('.ui-datepicker-current-day').click();

Here is the fiddle containing the changed code. You can open the console to see the change

https://jsfiddle.net/520vqz7g/

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

Comments

0

Try trigger('change')

$(document).ready(function() {
  $('#alltour').datepicker({
    dateFormat: 'dd-mm-yy',
    minDate: 0,
  }).on('change', function(dateText, inst) {
    console.log('selected date', $('#alltour').val());
  }).datepicker("setDate", new Date()).trigger('change');
});
<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.js"></script>

<input name="date" type="text" id="alltour" class="datepickerdate" placeholder="" value="">

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.