V5 Datepicker is triggering on change event on initialize

I noticed that the V5 date picker triggers an onchange event when it’s initialized. This is problematic because in one case I want to reload the page when the date is changed, but if simply initializing the date picker triggers an onchange event, which reloads the page again, I end up in an endless loop of page loads.

How do I prevent the onchange from firing on initialize?

Hi @Vincent_Wansink!

Can you share a simple example where this happens? I was unable to reproduce this. When I initialize the datepicker there is no change event happening.


o.k. I’ve simplified my code so you can see it here:


I’m initializing the date picker like so

var inst = $('#pagedate').mobiscroll5().datepicker({
    onChange: function(event,inst){
      console.log("onChange event firing now...");

Notice that in the onChange event I was calling a function (changePageDate) that would reload the page based on the new date (causing an endless loop of reloading). I’ve obviously commented it out here but instead I’m logging to the console “onChange event firing now…” so you can see that as soon as the page loads.

The only other thing I’m loading on this page is jquery and mobiscroll 4 and 5.

It also happens if I do it this way.


    console.log("page date changing now...");

I think it’s applying the format when it’s initialized and that’s what’s causing the change event to fire. It starts off with this format : 2021-03-30 but it’s immediately changed to this format: 03/30/2021. I can see the flicker.

Thanks @Vincent_Wansink for the example. Now I could figure out what the issue is. So basically, you have a value in the input initially, and then you initialize the datepicker on the input.

The first issue is that the date format is different for the datepicker, than of the initial value that’s in the input. Because of that the date is rewritten in the input so a change is triggered. This is the native change event on the input.

The second issue that triggers the datepicker’s onChange event is that internally the value changes to a Date object that is parsed from the input. In this case this shouldn’t happen, so we consider this a bug, and will solve this in the upcoming versions. As a workaround you can use the returnFormat option set to "locale" in order to have the internal value the same value you have in the input, OR you can pass the value option to the datepicker with a javascript Date object.

1st olution

<input value="03/30/2021" id="pagedate" />

  returnFormat: 'locale',
  onChange: function(ev, inst) {
    // not triggered initially

2nd solution

<input id="pagedate" value="03/30/2021" />

  value: new Date(2021, 2, 30),
  onChange: function(ev, inst) {
    // not triggered initially

I would recommend the first solution because it’s more convenient to just use the locale return format, that to build up a date object.