Search code examples
jquerykeyboard

jquery come back to previous value of an input with esc key


On a lot of applications, when typing an input, the use of the esc key returns the previous value. How can I do this with jquery.


Solution

  • Try this, it should do what you want.

    var prevValue = "";
    $("#Input").focus(function() {
      prevValue = $(this).val();
    }).keyup(function(e) {
      if (e.keyCode == 27) {
        $(this).val(prevValue);
      }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input value="hello" id="Input" />