Home > Blockchain >  How to check the increase/decrease of letter input in pasting clipboard in jQuery
How to check the increase/decrease of letter input in pasting clipboard in jQuery

Time:05-10

I can use the keyup() function to check that my user has written something in input. But what do I do when something pastes there?(Using the mouse, not the keyboard!)

$("#myInput").keyup(function(){
  console.log("increase or decrease now!");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input id="myInput" />

CodePudding user response:

The "keyup" event only fires after a (finished) keyboard input, which means it does not fire when you paste something with your mouse. You could instead use the "paste" event, which only fires after you paste something into the input field. But you can combine "keyup" and "paste" like this:

$("#myInput").on("keyup paste", function(){
  console.log("increase or decrease now!");
});

You could also think about the "change" event. This only fires after the input field was changed and loses focus. So, you could actually replace keyup paste by change or combine all of the three to be sure it is fired no matter how the user changes the input field by using keyup paste change.

Edit: Please note that the paste event does not work in Internet Explorer <= 10. (https://caniuse.com/mdn-api_window_paste_event)

  • Related