12

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?

1
  • Unrelated: Your closing tag is wrong, it should be </script>. Commented Dec 16, 2014 at 13:52

10 Answers 10

13

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);
        }
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

setting attribute readOnly may disable validation (if any) for this field. In my opinion, is best solution is set attr onkeydown="false"
6

Try using the onSelect function like:

$(function() {
    $( ".datepicker" ).datepicker({
        onSelect: function() {
        //your code here
    }});

This will be fired when they select a date from the calendar not when they are typing.

Comments

3

You can have onSelect trigger the change event on the text field. This way editing the date via the text box or via the date picker behave the same.

$('.inp').datepicker({
  onSelect: function() {
    return $(this).trigger('change');
  }
});
    
$(".inp").on("change",function (){ 
   console.log('inp changed');
});

Comments

0

If you are using the bootstrap datepicker you can do the following:

$('.datepicker').datepicker()
    .on('changeDate', function(e){
        # `e` here contains the extra attributes
});

For more details view: Bootstrap Datepicker events

Comments

0

Thanks! Everybody, but I could not make field readonly one, this is requirement. So I had to reasearch myself. So I posting my final solution. I did unbinding for 150 ms, then bind again. I made a jquery plugin for this

function myfunc(that){
        $(that).one('change', function(){
                var that = this;
                setTimeout(function(){myfunc(that);}, 150);
                //some logic
            }
        )
}

myfunc(".inp");

Comments

0

Thank you everyone, this is what I used though,

function myfunction(x,x2){
    var date1 = new Date(x);
    var MyDateString; 
    MyDateString = date1.getFullYear()+ '/' + ('0' + (date1.getMonth()+1)).slice(-2) + '/' + ('0' + date1.getDate()).slice(-2);
    x2.value=  MyDateString;    
 }

x, being the datepicker value and x2 being the datepicker object

Comments

0

Try this

  $(function() {
        $("#birth_date").datepicker({
        onSelect: function() {
            console.log("You Code Here");
        }});
    });

Comments

0

If you are using datepiker ui you can also used following formate

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

onSelect: function(date) {

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

1 Comment

Thanks for your contribution, @krunal, but it would help to add some context around why this will work.
0
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css" type="text/css"/>
<script src="js/jquery-1.12.4.js"></script>
<script src="js/jquery-ui.js"></script>
<script>
    var j = jQuery.noConflict();
    j(function() {
        j("#datepickerFrom").datepicker({
            changeMonth: true,
            changeYear: true,
            dateFormat: "yy/mm/dd",
            onSelect: function () {
                var date = $(this.value).selector;
                document.getElementById("dateFrom").value = date;
            },
        }).on("change",function (){
            var date = $(this.value).selector;
            document.getElementById("dateFrom").value = date;
        })
        .datepicker("setDate",document.getElementById("dateFrom").value);
});
</script>

<input id="datepickerFrom"/>
<input type="hidden" name="dateFrom" id="dateFrom"/>

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-2

if you are using date picker of jquery there is no need of calender event fire. you can fire the event on textbox "textchanged"

1 Comment

if you are using textbox in calender then you cau use this above approach.

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.