Iqbal Pasha
Iqbal Pasha

Reputation: 1316

Jquery if keydown and keyup event trigger disable other keypress events works in Chrome not in IE & Mozilla

As i'm creating autosuggestions text box but i'm facing issue as when keydown and keyup trigger then it is also triggering keypress event in Mozilla and IE But it works fine in Chrome.

$(".search-terms").on("keypress", function(){
  $("#notice").html("Searching");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="text" value="Search items you want to exchange" autocomplete="off" name="resultsFor" class="search-terms">
<div id="notice">Ready to search</div>

Upvotes: 0

Views: 406

Answers (1)

Ram Segev
Ram Segev

Reputation: 2571

Ok i have found the issue debugging it like this

$(".search-terms").keypress(function( event ) {
  $("#notice").html(event.keyCode);
  
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="text" value="Search items you want to exchange" autocomplete="off" name="resultsFor" class="search-terms">
<div id="notice">Ready to search</div>

As you can see you get the keycode value, when using the same code in chrome nothing happens but in firefox for the arrows keys you get values from 37-40. So with using if statement you can decide what to do with if, here is an example

$(".search-terms").keypress(function( event ) {
//$("#notice").html(event.keyCode);
if (event.keyCode >= 37 && event.keyCode <= 40) 
    {
        //do something
    }
    else
    {
 		 $("#notice").html("Searching");
  	}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="text" value="Search items you want to exchange" autocomplete="off" name="resultsFor" class="search-terms">
<div id="notice">Ready to search</div>

Upvotes: 1

Related Questions