werva
werva

Reputation: 1649

Clear html input preserving back(ctrl+z)

When I use jquery to clear content of input:

$('input#myinput').val('');

everything is fine except for the user cannot use ctrl+z to obtain the previous text. is there any way to preserve ctrl+z ?


is there any way behaves as if it is cleared by user?

Upvotes: 3

Views: 141

Answers (1)

user2181177
user2181177

Reputation:

Suppose it gets cleared on clicking a button:

$(document).ready(function() {
  var input = $('input#myinput').val();
  $('#button').click(function() {
    $('input#myinput').val('');
  });
  $(document).keydown(function(e) {
    if (e.which === 90 && e.ctrlKey) {
      $('input#myinput').val( input );
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="myinput" value="text"/>
<input type="button" id="button" value="Button" />

Upvotes: 2

Related Questions