jQuery-Mask-Plugin icon indicating copy to clipboard operation
jQuery-Mask-Plugin copied to clipboard

Fix onChange not firing when typing previous value

Open thiagorb opened this issue 6 years ago • 4 comments

The current implementation is not firing the onChange callback passed in the options.

This happens because the oldValue variable is updated only on blur. With this changes, two variables are used. One controls the value that the element had when it got focus, and the other one is changed immediately after every change.

Fiddle that reproduces the problem: https://jsfiddle.net/kp5hq1e8/7/ Steps to reproduce:

  • Click on the input to give focus
  • Type something (for example "1")
  • Click somewhere else on the page so the input loses focus
  • Click again on the input
  • Type another value ("12")
  • Type the previously typed value ("1")

It was expected that the onChange callback would be fired every time, but when entering the value that the input had when it lost focus nothing happens.

Output of the fiddle with the steps above:

  • focus
  • 1
  • blur
  • focus
  • 12
  • 12
  • 12
  • blur

Expected output:

  • focus
  • 1
  • blur
  • focus
  • 12
  • 1
  • 12
  • 1
  • 12
  • blur

thiagorb avatar Apr 15 '18 21:04 thiagorb

Thanks for this @thiagorb

I will give it a shot ASAP.

igorescobar avatar Apr 18 '18 09:04 igorescobar

I really wants to be merged. Same issues has been raised previously.

https://github.com/igorescobar/jQuery-Mask-Plugin/issues/358 https://github.com/igorescobar/jQuery-Mask-Plugin/issues/486

Another solution is to introduce a new callback onInput that will be called every time without comparing oldValue.

ankurk91 avatar Aug 06 '18 06:08 ankurk91

@ankurk91 have you tested this PR?

igorescobar avatar Aug 08 '18 08:08 igorescobar

@igorescobar I have tested it, and it works as described.

ankurk91 avatar Aug 08 '18 08:08 ankurk91