javascript - Prevent spaces in input field on keypress event -


i'm using following code detect multiple keys on keypress event:

var down = []; $(document).keydown(function (e) {      down[e.keycode] = true; }).keyup(function (e) {      if (down[17] && down[32]) {           //      }      down[e.keycode] = false; }); 

however, hotkey (ctrl + space) meant used while input field has focus. whenever press key combination, adds space input field.

how can prevent happening? i've looked @ ways disable spaces in input (like this), can't figure out how make work inside keypress event only.

i ended using different approach, melanciauk suggested.

on keyup event, removes last character in input field.

var down = []; $(document).keydown(function (e) {      down[e.keycode] = true; }).keyup(function (e) {      if (down[17] && down[32]) {           //           input = $(':focus');           input.val(function (index, value) {                return value.substr(0, value.length - 1);           });      }      down[e.keycode] = false; }); 

while doesn't prevent space being added, removes immediately.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -