Home > database >  DatePicker onClose returns undefined value
DatePicker onClose returns undefined value

Time:05-17

I have an issue with jquery datepicker in materialize, it always returns an undefined value in the onClose function:

<input type="text" >

...


$('.datepicker').datepicker({ 
  format: 'yyyy-mm-dd',
  firstDay: 1,
  minDate: new Date(),
  autoClose: true,
  onClose: function (date, datepicker) {
    alert("Selected Date: "   date);
  }
});

date is always undefined - but the input field is correctly filled with the date, e.g. 2022-05-16

Did I do something wrong? How can I get the value?

The input field does not have an ID, because it is created dynamically.

CodePudding user response:

I reproduced your snippet, and as you can see it works fine, as long as you are not able to reproduce the issue it will be impossible to help you further.

$('.datepicker').datepicker({
  format: 'yyyy-mm-dd',
  firstDay: 1,
  minDate: new Date(),
  autoClose: true,
  onClose: function(date, datepicker) {
    alert("Selected Date: "   date);
  }
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.13.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-3.6.0.js"></script>
<script src="https://code.jquery.com/ui/1.13.1/jquery-ui.js"></script>

<input type="text" >

CodePudding user response:

Works for me:

$('.datepicker').datepicker({ 
  format: 'yyyy-mm-dd',
  firstDay: 1,
  minDate: new Date(),
  autoClose: true,
  onClose: function (date, datepicker) {
    alert("Selected Date: "   date);
  }
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>

<input type="text" >

  • Related