Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery datepicker change event trigger and input's default change event

I have datepicker

  <input type='text' class='inp'>  
<script>
   $('.inp').datepicker();

  $(".inp").on("change",function (){ 
   console.log('inp changed');
  });
</script>

When I first change '.inp' manually type there a value, then immediately I click on datepicker's opened calendar. I get two 'change' event listeners. First from manual change, then from datepicker change. How can I avoid this?

like image 953
Konstantin Vahrushev Avatar asked Dec 16 '14 13:12

Konstantin Vahrushev


1 Answers

Set your input readOnly, it will help you to change the value of field through icon only.

<input type='text' class='inp' readOnly />

then use onSelect to get selected date, as following:

$(function() {
    $(".inp").datepicker({
        onSelect: function(dateText, inst) {
            // alert(dateText);
        }
    });
});
like image 60
Parkash Kumar Avatar answered Oct 03 '22 23:10

Parkash Kumar