1

I use easyautocomplete plugin(http://easyautocomplete.com/guide), I use some custom JavaScript to bind keydown event to search input which redirect the page to suggestion page when user press "ENTER" , But it redirects the page even when the user selects an item from suggestion list and press Enter. easyautocomplete provides an "onKeyEnter" event which only triggers when we press "Enter" after selecting an item from suggestions

So this is the custom code I added

 jQuery("#input_field").keydown( {
  if(e.which == 13) {
    document.location = "/search_results.php";
  }
});

So I need to stop this redirection when we press Enter after choosing any item from suggestion list by up arrow and down arrow key.

easyautocomplete has already an event handler to capture the "Enter" press on list suggestions only, i.e that event function will not be captured when we simply press Enter on the input field after manually writing something

So what can I do to prevent that redirect in that eventHandler's function

 onKeyEnter : function(){
     // stop redirect
    .........
    .......
   }

I don't want to permanently remove the "redirect " action from the input, just want to avoid it in particular case that is why I can't use .off()

Thanks!

UPDATE

I am not sure if it is the best method but window.stop() did the job for me

  jQuery("#input_field").keydown( {
  if(e.which == 13) {
    document.location = "/search_results.php";
  }
   });


 onKeyEnter : function(){ // press Enter on the search suggestions
     window.stop(); // stop the redirecting 
        }

Thanks!

Mosè Raguzzini
  • 15,399
  • 1
  • 31
  • 43
nikhil123
  • 48
  • 1
  • 10

1 Answers1

1

You can introduce global variable like:

let canRedirect = true;

and for stop redirect, set it to false. In keypress handler just extend the condition:

if (e.which == 13 && canRedirect) {}
hsz
  • 148,279
  • 62
  • 259
  • 315
  • Where should I define canRedirect variable ?, I think I need to look through the easyautocomplete plugin's javascript and find the right place for define the varible, and it will also need to be added each time I update the plugin, isn't there a better solution?, Thanks – nikhil123 Jul 31 '18 at 09:43
  • in fact, after thinking about it more, I realized this method can not be used to solve my question, Even if it put the code in easyautocomplete's javascript, – nikhil123 Jul 31 '18 at 10:18