igorescobar / jQuery-Mask-Plugin

A jQuery Plugin to make masks on form fields and HTML elements.
http://igorescobar.github.io/jQuery-Mask-Plugin/
Other
4.77k stars 1.42k forks source link

jQuery 'input' event receiving unexpected value #766

Open luis-fss opened 3 years ago

luis-fss commented 3 years ago

Device

Windows 10

Browser (and version)

Google Chrome v88.0.4324.190 64 bits

Functional jsfiddle exemplifying the problem:

https://jsfiddle.net/Lukkian/tgqjof23/4/

Describe the problem:

If 'applyMask()' is called before setting up the 'input' event, pressing any letter key will hit the event, but if 'applyMask()' is called after setting up the 'input' event, pressing any letter key will not hit the event.

jQuery('.btn').on('click', function(){
    jQuery('#MyField').unmask().mask('#.##0,00', { reverse: true })
})

function applyMask() {
    jQuery('#MyField').mask('#.##0,00', { reverse: true })
}

// applyMask() // here, before the input event, this function will behave nicely

jQuery('#MyField').on('input', function() {
  let val = jQuery(this).val()
  jQuery('.change-event-values').prepend('<p>'+ val +'</p>')
})

applyMask() // Here, after the input event, any letter will pass through the event.