user3374309 user3374309 - 17 days ago 7
Javascript Question

Disable input when enter key pressed multiple times

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') {


}
}

Answer

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.

Comments