javascript - Keypress event listener not working -


i'm having hard time trying understand how javascript events works. have until now:

$ ->  masks = document.queryselectorall('[data-mask]')   in masks   i.addeventlistener("keypress", (e) -> onlynumber(e) )   mask = i.getattribute("data-mask")    switch (mask)    when "cep" i.addeventlistener("keypress", -> cep(this)); i.setattribute("maxlength", "9")    cep = (input) ->      input.value = input.value + "-" if input.value.length == 5 , input.charcode != 8 # backspace  onlynumber = (event) ->  event.preventdefault() if event.charcode > 0 , (event.charcode < 48 || event.charcode > 57 ) 

what need correct line:

input.value = input.value + "-" if input.value.length == 5 , input.charcode != 8 # backspace 

i think event added inside switch block may not able answer key pressed, i'm not sure if can solve problem way. besides conditional in code:

input.charcode != 8 # backspace 

the rest working!

edit1: documentação .keypress "the keypress event sent element when browser registers keyboard input. similar keydown event, except modifier , non-printing keys such shift, esc, , delete trigger keydown events not keypress events. other differences between 2 events may arise depending on platform , browser." it's seems should use keydown event recognize backspace key.


Comments

Popular posts from this blog

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

android - ConstraintLayout: Realign baseline constraint in case if dependent view visibility was set to GONE -

c# - Populating Gridview inside Listview ItemTemplate On Web User Control from Code Behind -