Avoid the redirect action bind by previous javscript code

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Avoid the redirect action bind by previous javscript code



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 click enter,
But it also redirect the page when user select any item from suggestion list that appear and press Enter. easyautocomplete provides a "onKeyEnter" event which only triggers when we press "Enter" after selecting any item from suggestions



So this is the custom code I added


$(document).keypress(function(e) {
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 a event handler to capture the "Enter" press on for 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()


.off()



Thanks!




1 Answer
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:


false


keypress


if (e.which == 13 && canRedirect) {}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived