From f716683c2ac0a9e98c5c7f9e56e8ec34fa455bb7 Mon Sep 17 00:00:00 2001 From: Thiago Barcala Date: Sun, 15 Apr 2018 23:37:00 +0200 Subject: [PATCH] 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. --- src/jquery.mask.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/jquery.mask.js b/src/jquery.mask.js index 019f2ec2..11664042 100755 --- a/src/jquery.mask.js +++ b/src/jquery.mask.js @@ -112,15 +112,15 @@ el.data('changed', true); }) .on('blur.mask', function(){ - if (oldValue !== p.val() && !el.data('changed')) { + if (focusValue !== p.val() && !el.data('changed')) { el.trigger('change'); } el.data('changed', false); }) // it's very important that this callback remains in this position - // otherwhise oldValue it's going to work buggy + // otherwhise focusValue it's going to work buggy .on('blur.mask', function() { - oldValue = p.val(); + focusValue = p.val(); }) // select all text on focus .on('focus.mask', function (e) { @@ -377,6 +377,7 @@ options[name].apply(this, args); } }; + oldValue = val; callback('onChange', changed === true, defaultArgs); callback('onKeyPress', changed === true, defaultArgs); @@ -386,7 +387,7 @@ }; el = $(el); - var jMask = this, oldValue = p.val(), regexMask; + var jMask = this, focusValue = p.val(), oldValue = p.val(), regexMask; mask = typeof mask === 'function' ? mask(p.val(), undefined, el, options) : mask;