timpone
timpone

Reputation: 19929

jQuery on keyup and ignoring specific events

I have the following but it is not working. Does the jQuery event support the keyCode or do I need to get that someplace else?

$('#search-query').on('keyup',function(event){
  if(event.keyCode!==38 or event.keyCode!==40){
    search_autocomplete.call(this);
  }
});

How would I tell jQuery to just ignore if it's keyCode 38 or 40? Ultimately, I want to get this to like the search bar on http://www.linkedin.com/. Maybe just bootstrap it? http://twitter.github.com/bootstrap/javascript.html#typeahead

thx in advance

Upvotes: 0

Views: 348

Answers (3)

Ewen
Ewen

Reputation: 401

Have a look at keypress...

$('#search-query').keypress(function (e) {
    if(e.which == 38  || e.which == 40) 
        search_autocomplete.call(this);
    }
});  

Not tested but hopefully this helps

Upvotes: 0

Sushanth --
Sushanth --

Reputation: 55740

Make sure you use both event.keyCode and event.which for cross browser compatibility

$('#search-query').on('keyup',function(event){

   var key = event.keyCode || event.which;

  if(key !==38 || key !==40){
    search_autocomplete.call(this);
  }
});

Upvotes: 2

Adil
Adil

Reputation: 148110

Use || logical operator instead of or to join the condition in if statement.

Live Demo

Change

 if(event.keyCode!==38 or event.keyCode!==40){

To

 if(event.keyCode!==38 || event.keyCode!==40){

Upvotes: 2

Related Questions