Search code examples
jquerydatepickertimepickeranytime

Anytime time / datepicker triggers change event each time an input box is focused even without changing the content. How to avoid this?


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


Solution

  • 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.