Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent unwanted change event when page loads #149

Closed
asumaran opened this issue May 21, 2014 · 7 comments
Closed

Prevent unwanted change event when page loads #149

asumaran opened this issue May 21, 2014 · 7 comments

Comments

@asumaran
Copy link

I have a form with a pre-populated date input which i'm listen the change event to fetch results via ajax.

Happens every time the page is loaded the date input triggers the change event, which makes an unnecesary ajax request because the page is loading with results already.

Is there any way to avoid or silence the event only when the page just finished to load?

I'm wondering this because the prepopulated value is '21/05/2014' and the value after the change event remains '21/05/2014'

This is the code i'm using to configure Pikaday

$(function(){
  var picker = new Pikaday({
    field: document.getElementById('date'),
    format: "DD/MM/YYYY"
  });
});
@asumaran
Copy link
Author

after some test I noticed this only happens when Pikaday is configured inside of a $() function.
So I put the initialization code to just before </body> and works well. The only thing that bothers me is the code have to be in the html file and not in a separate JS file.

@rikkert
Copy link
Member

rikkert commented May 26, 2014

Great, thanks.

@rikkert rikkert closed this as completed May 26, 2014
@SeanRoberts
Copy link

Is there a way to get around this without having to put the code in the HTML? I'm using Pikaday as part of an Ember component so putting the initializer into the HTML isn't really an option. Are there are pika-specific events I can watch for?

@shyamchandranmec
Copy link

Any solution to this?

@Niarike
Copy link

Niarike commented Nov 4, 2015

I was able to fix this without putting the initialization in the HTML by editing the pikaday.js file on line 730 where there's a fireEvent call...I wrapped it with an if statement checking to see if 'preventOnSelect' was true because the initialization case was the only one I could find where true was sent in rather than null/false. Code (with a few surrounding lines for clarity) looks like this:

728:  if(this._o.field) {
729:      this._o.field.value = this.toString();
730:      if (!preventOnSelect) {
731:            fireEvent(this._o.field, 'change', { firedBy: this });
732:      }
733:  }

@Yatekii
Copy link

Yatekii commented Jul 30, 2018

Can you please reopen the issue and fix this?

I actually have a case where this behavior causes a stackoverflow as it will call the onchange event over and over again.
This is not really working with vdom libs (mithril in my case), I guess.

I don't see why the onchange event should be emitted when nothing actually changed.

Thanks a ton!

bluebinary added a commit to bluebinary/Pikaday that referenced this issue Apr 13, 2019
Edited setDate to resolve issue Pikaday#149 where Pikaday unintentionally fires a 'change' event on page load, if the input form field associated with the Pikaday component contains a pre-filled value set from prior user input. The fix at lines 852-854 instead have the library temporarily cache the old value before setting the new value, then the old and new are compared before fireEvent() is called to trigger the 'change' event. The library will now only fire the 'change' event if the value has changed.

This has been successfully tested to resolve issue 149. Please feel free to accept the PR or adjust as desired.
@bluebinary
Copy link

I had the same issue, and found that even the most recent version of Pikaday fires the change event on page load if the form field that is configured as a Pikaday field has a pre-populated date from previous user-input.

I found that by modifying pikaday.js on lines 852-854 (of the current committed file as of writing), to include an old/new value comparison around the fireEvent() call, that the change event now only fires when the data actually has changed.

Feel free to merge the changes referenced in the above forked commit to resolve #149.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants