jQuery-Mask-Plugin
jQuery-Mask-Plugin copied to clipboard
Fix onChange not firing when typing previous value
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
Thanks for this @thiagorb
I will give it a shot ASAP.
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 have you tested this PR?
@igorescobar I have tested it, and it works as described.