Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Tuesday, November 1, 2016

JQuery Datepicker onchange event help!

JQuery Datepicker onchange event help!


I have JS code that when you change a field it will call a search routine.... the problem is I can't find a Jquery event that will fire when the Datepicker updates the input field.

For some reason a change event isn't called when datepicker updates the field. When the calendar pops up it changes the focus so I can't use that either. Any ideas?

Answer by T.J. Crowder for JQuery Datepicker onchange event help!


You can use the datepicker's onSelect event.

$(".date").datepicker({    onSelect: function(dateText) {      display("Selected date: " + dateText + "; input's current value: " + this.value);    }  });  

Live example:

jQuery(function($) {      $(".date").datepicker({      onSelect: function(dateText) {        display("Selected date: " + dateText + "; input's current value: " + this.value);      }    }).on("change", function() {      display("Got change event from field");    });      function display(msg) {      $("

").html(msg).appendTo(document.body); } });

      

Unfortunately, onSelect fires whenever a date is selected, even if it hasn't changed. This is a design flaw in the datepicker: It always fires onSelect (even if nothing changed), and doesn't fire any event on the underlying input on change. (If you look in the code of that example, we're listening for changes, but they aren't being raised.) It should probably fire an event on the input when things change (possibly the usual change event, or possibly a datepicker-specific one).


If you like, of course, you can make the change event on the input fire:

$(".date").datepicker({    onSelect: function() {      $(this).change();    }  });  

That will fire change on the underlying inputfor any handler hooked up via jQuery. But again, it always fires it. If you want to only fire on a real change, you'll have to save the previous value (possibly via data) and compare.

Live example:

jQuery(function($) {      $(".date").datepicker({      onSelect: function(dateText) {        display("Selected date: " + dateText + "; input's current value: " + this.value);        $(this).change();      }    }).on("change", function() {      display("Got change event from field");    });      function display(msg) {      $("

").html(msg).appendTo(document.body); } });

      

Answer by locrizak for JQuery Datepicker onchange event help!


Your looking for the onSelect event in the datepicker object:

$('.selector').datepicker({     onSelect: function(dateText, inst) { ... }  });

Answer by user583576 for JQuery Datepicker onchange event help!


$('#inputfield').change(function() {       dosomething();  });  

Answer by CodeDreamer68 for JQuery Datepicker onchange event help!


I wrote this because I needed a solution to trigger an event only if the date changed.

It is a simple solution. Each time the dialog box closes we test to see if the data has changed. If it has, we trigger a custom event and reset the stored value.

$('.datetime').datepicker({      onClose: function(dateText,datePickerInstance) {          var oldValue = $(this).data('oldValue') || "";          if (dateText !== oldValue) {              $(this).data('oldValue',dateText);              $(this).trigger('dateupdated');          }      }  });  

Now we can hook up handlers for that custom event...

$('body').on('dateupdated','.datetime', function(e) {      // do something  });  

Answer by theChrisKent for JQuery Datepicker onchange event help!


T.J. Crowder's answer (http://stackoverflow.com/a/6471992/481154) is very good and still remains accurate. Triggering the change event within the onSelect function is as close as you're going to get.

However, there is a nice property on the datepicker object (lastVal) that will allow you to conditionally trigger the change event only on actual changes without having to store the value(s) yourself:

$('#dateInput').datepicker({       onSelect: function(d,i){            if(d !== i.lastVal){                $(this).change();            }       }  });  

Then just handle change events like normal:

$('#dateInput').change(function(){       //Change code!  });  

Answer by user2442912 for JQuery Datepicker onchange event help!


DatePicker selected value change event code below

/* HTML Part */  

Date:

/* jQuery Part */ $("#datepicker").change(function() { selectedDate= $('#datepicker').datepicker({ dateFormat: 'yy-mm-dd' }).val(); alert(selectedDate); });

Answer by mavirroco for JQuery Datepicker onchange event help!


if you are using wdcalendar this going to help you

 $("#PatientBirthday").datepicker({           picker: "",          onReturn:function(d){            var today = new Date();            var birthDate = d;            var age = today.getFullYear() - birthDate.getFullYear();            var m = today.getMonth() - birthDate.getMonth();            if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {              age--;            }              $('#ageshow')[0].innerHTML="Age: "+age;            $("#PatientBirthday").val((d.getMonth() + 1) + '/' + d.getDate() + '/' +  d.getFullYear());          }        });   

the event onReturn works for me

hope this help

Answer by mike tracker for JQuery Datepicker onchange event help!


Try this

$('#dateInput').datepicker({   onSelect: function(){         $(this).trigger('change');        }   }});  

Hope this helps:)

Answer by seeeking for JQuery Datepicker onchange event help!


On jQueryUi 1.9 I've managed to get it to work through an additional data value and a combination of beforeShow and onSelect functions:

$( ".datepicker" ).datepicker({      beforeShow: function( el ){          // set the current value before showing the widget          $(this).data('previous', $(el).val() );      },        onSelect: function( newText ){          // compare the new value to the previous one          if( $(this).data('previous') != newText ){              // do whatever has to be done, e.g. log it to console              console.log( 'changed to: ' + newText );          }      }  });  

Works for me :)


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.