When there is focus at input and text is entered. Then when you press the up and down buttons, the cursor moves to the beginning or end of the line. How can this be prevented? I add at event keyup stopPropagation and preventDefault.

$(me.inputSelector).on('keyup', function(e) { if (e.keyCode == 38) { // вверх e.stopPropagation(); e.preventDefault(); return; } if (e.keyCode == 40) { // вниз e.stopPropagation(); e.preventDefault(); return; } }); 
  • Are you sure that you rightly wrote e.KeyCode ?? Page Up - 33 Page Down - 34 - Arsen
  • No, I mean the up and down arrows. - manking

1 answer 1

Need to hang on keydown:

 document.querySelector('input').addEventListener('keydown', e => { if (e.keyCode == 38 || e.keyCode == 40) { e.stopPropagation(); e.preventDefault(); return false; } }); 
 <input value='1234567890' /> 

At the time of the keyup event, the click is already processed by the browser.