1

How do I make so that when the enter key is pressed multiple times, the site only detects the input once? I have a small bug which occurs whenever the user double taps the enter key in an input box, so what I want to do is only accept the input press once. Thank you so much in advance!!

  $('#search_list').keypress(function(e) {
    if (e.which == '13') {


   }    
}
user3374309
  • 141
  • 2
  • 10

2 Answers2

3

You can prevent repeatedly enter pressing by this way:

var enterPressed = false;

$('#search_list').keypress(function(e) {
    if (e.which == '13') {
       if( ! enterPressed){
         // do some processing
         return;
       }
      enterPressed = true;
      setTimeout(function(){
         enterPressed = false;
      }, 1000);

   }    
}

Inspired by this post.

Community
  • 1
  • 1
Walid Ammar
  • 4,038
  • 3
  • 25
  • 48
0
var enterPressed = false;

$('#search_list').keypress(function(e) {

    if (e.which == '13') {
       if( ! enterPressed){
         // do some processing

          enterPressed = true;
          setTimeout(function(){
            enterPressed = false;
          }, 1000);

        }


   }    
}

I think this way is better.