0

As stated in the title, I get a change event each time I focus an input text box with an anytime datepicker / timepicker attached. Is there a way to avoid this behaviour? Since I obviously only want to get a change event, when I actually change something.

I see this behaviour on firefox and chrome. Didn't try IE

Toskan
  • 13,911
  • 14
  • 95
  • 185

2 Answers2

2

If it's really doing that, it's simple enough to remember the previous value somewhere and only call your "real" change function when the value doesn't match.

$("relevant-selector").on("change", function() {
    var $this = $(this),
        val   = $this.val(),
        prev  = $this.data("prevValue");
    if (val !== prev) {
        handleChange();
        $this.data("prevValue", val);
    }
});

You might also report a bug to the plugin developers. Or, if it's open source, report it, fix it, and send them a pull request.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I actually used this to store the value ` `$("relevant-selector").each(function(i, el){ var jel = $(el); jQuery.data(jel[0], "prevValue", jel.val());` – Toskan May 27 '14 at 22:06
  • @Toskan: Using `data` is a *much* better idea than my original example, nice one! Side note: `$` and `jQuery` refer to the same thing (unless you're using `noConflict`), so it's usually best to use one or the other rather than mixing. Also, `data` is available as an instance method on jQuery sets, so `jel.data("prevValue, ...)` works. I've updated my example to use `data`. Best, – T.J. Crowder May 28 '14 at 07:09
  • The real issue is that the picker parses the value in the field when it is first displayed, then makes any necessary adjustments to the value according to the parameters you specify, then updates the field to display the adjusted value--THAT is what causes the change event to fire (apparently, the browser can't tell that the updated value is the same as the old value). I agree that remembering the old value (as suggested here) is the best way to decide whether the field has really changed or not. – Andrew M. Andrews III May 29 '14 at 14:58
  • 1
    @AndrewM.AndrewsIII: Putting a value in an `input` field via JavaScript (e.g., what the datepicker does) doesn't trigger a `change` event. `change` only fires if the *user* changes the value, or if someone fires it on purpose. Presumably, in this case, the datepicker is firing it on purpose (despite it not having changed). – T.J. Crowder May 29 '14 at 16:18
  • 1
    @"T.J. Crowder" you are soooo right, mea culpa... Toksan, I have modified the library to only trigger a change() event when the value of the input field actually changes. Please download version 5.0.2 from http://www.ama3.com/anytime/ and please send me a message via my Contact page to let me know how it works for you. – Andrew M. Andrews III May 30 '14 at 14:40
2

@Toksan, I have modified the library to only trigger a change() event when the value of the input field actually changes. Please download version 5.0.2 from ama3.com/anytime and please send me a message via my Contact page to let me know how it works for you.

If you have future problems, questions or suggestions regarding the library, please send me a message via my Contact page for faster resolution.

Andrew M. Andrews III
  • 1,989
  • 18
  • 23