1

I need to get the value of datepicker on change, But it is returning undefined.

HTML :

<input  id="#return" name="start" class="date-pick form-control" value="" data-date-format="dd/mm/yyyy" type="text" />

JS :

$('input.date-pick').datepicker().on('changeDate', function (ev) {
      var firstDate = $('#return').val();
      alert(firstDate);
});
Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
vellai durai
  • 1,019
  • 3
  • 16
  • 38
  • 1
    You've asked the [same question](http://stackoverflow.com/questions/38450944/how-to-get-the-value-of-date-picker-on-change-in-jquery) 5 hours earlier... – Andreas Jul 19 '16 at 11:48
  • Here is an [answer](http://stackoverflow.com/questions/6471959/jquery-datepicker-onchange-event-help) in stackoverflow too !!! – Kirankumar Dafda Jul 19 '16 at 11:49

6 Answers6

5

You could also use regular event change :

$('input.date-pick').datepicker().on('change', function (ev) {
   var firstDate = $(this).val();
   alert(firstDate);
});

Hope this helps.

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
4

Use onSelect event and you can get selected value with in dateText variable

$('.date-pick').datepicker({
    onSelect: function(dateText, inst) {
      alert(dateText);
    }
});
Dhara Parmar
  • 8,021
  • 1
  • 16
  • 27
  • @KirankumarDafda The inst parameter is an internal object that captures the current state of the datepicker. Normally you don't need to use it at all. Inside the callback – Dhara Parmar Jul 19 '16 at 11:51
3

you can easily do with below code:

 $(".date-pick").on('change', function(event) {
   event.preventDefault();
   alert(this.value);
  /* Act on the event */
  });
Therichpost
  • 1,759
  • 2
  • 14
  • 19
0

Your date field

<input id="thedate" type="text" class="date-pick"/>

And if you using bootstrap datepicker then add this script code

$(function() {

  $('.date-pick').datepicker({
    dateFormat: 'dd-mm-yy',
    onSelect: function(dateText, inst) {
      alert(dateText);
    }
  });

});

Here is the fiddle

http://jsfiddle.net/s1L5pjpb/1/

Punit Gajjar
  • 4,937
  • 7
  • 35
  • 70
0

You can also get the value directly from your onchange event:

onChange="variable=$(this).val();"
THess
  • 1,003
  • 1
  • 13
  • 21
0
$("#datepickerID").on('change', function() {
  dateVar = $("this").val();
}

You can get value by calling function when onchange event occur. Then, after you can store that value, alert it or print it on console.

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Demon
  • 21
  • 1