javascript - Place cursor at the end of text on keyup -


i'm making same functionality command line has. when enter commands , press key button, presented previous command.

now have working , works fine i've encountered 1 small problem.

the problem encounter when pressing up key, cursor placed @ beginning of command, while when using down button, cursor placed @ end of comand.

expected behaviour cursor placed @ end both.


what makes different other questions?

  • the focus on input field.
  • it work down button

settimeout(function() {  var inputs = document.queryselectorall('input');    inputs[0].onkeydown = function(e){    //keyup    if(e.keycode == 38) {      inputs[inputs.length-1].value = 'up pressed';    }        if(e.keycode == 40 ) {        inputs[0].value = 'down pressed';    }  }  }, 1000);
<!doctype html>  <html>      <head>      <link rel="stylesheet" href="style.css">      <script src="script.js"></script>    </head>      <body>      <input type="text" placeholder="press or down key"/>    </body>    </html>

settimeout(function() {  var inputs = document.queryselectorall('input');    inputs[0].onkeydown = function(e){    //keyup    if(e.keycode == 38) {      // placed here, prevents key's default behaviour go @ end of input text      e.preventdefault();      inputs[inputs.length-1].value = 'up pressed';    }        if(e.keycode == 40 ) {        inputs[0].value = 'down pressed';    }    }  }, 1000);
<!doctype html>  <html>      <head>      <link rel="stylesheet" href="style.css">      <script src="script.js"></script>    </head>      <body>      <input type="text" placeholder="press or down key"/>    </body>    </html>


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -