Ajax dropdown selection using arrow key

I have created an ajax dropdown, i need to change the background color of the dropdown values when i press the down arrow and up arrow key.Here when i press the key background changes and it disappears immediately.it's not working in the ajax dropdown, if i put and alert before setting the class selectedhash, it's working else it's not working.

Here the div will be updated by the ajax results with list.

Please help me to solve this.

  <div class='textautocomplete'>
  </div>


 $(document).on("keydown", function(e) { 
 if (e.keyCode == 40) 
 { 
 if(chosen === "")  
 {
 chosen = 0;

 } else if((chosen+1) < $('.textautocomplete ul').length) 
 {
 chosen++; 
 }
 $('.textautocomplete ul').removeClass('selectedhash');
 $('.textautocomplete ul:eq('+chosen+')').addClass('selectedhash');

 return false;
 }
 if (e.keyCode == 38) { 
 if(chosen === "") {
 chosen = 0;
 } else if(chosen > 0) {
 chosen--;            
 }
 $('.textautocomplete ul').removeClass('selectedhash');
 $('.textautocomplete ul:eq('+chosen+')').addClass('selectedhash');
 return false;
 }
 });


  $(".textinput").live("keyup",function(e)
  {
  $.post('/users/getusers',{data:dataString},function(result){
  if(result!=='') 
  {
  $('.textautocomplete').show();
  $('.textautocomplete').html(result);
  }
  else
  {
  $('.textautocomplete').hide();
  $('.textautocomplete').html('');
  }
  });
  return false
  });

Answers


Firstly change: ".live" to: ".on" in line:

$(".textinput").live("keyup",function(e)

because:

"As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live()."

More: jQuery .live() method

Secondly add keyCode filter code in:

$(".textinput")

For example:

if ( e . keyCode == 38  || e . keyCode == 40 ) { return false; }

Working example fiddle: JSFiddle


Need Your Help

Hive: Multiple files in one partition

hadoop hive

Hive: Can I add partition with few locations?

C2DM handle registration properly

android registration android-c2dm

I want to use C2DM in my android application. I am using code schemas shown in official tutorials. Everything seems to work fine, but I have problem with handling registration properly.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.