Why is the keydown event triggering more than one time
<p>I am trying to create a suggestion list under a text input field.  Currently I have the following code for the html of this list.</p>

<pre><code>&lt;div id="assigned_user_wrap"&gt;
   &lt;input type="text" name="item" id="item" value="" /&gt;
&lt;/div&gt;
&lt;div id="suggestions"&gt;
   &lt;ul&gt;
      &lt;li id="1"&gt;Item 1&lt;/li&gt;
      &lt;li id="2"&gt;Item 2&lt;/li&gt;
      &lt;li id="3"&gt;Item 3&lt;/li&gt;
      &lt;li id="4"&gt;Item 4&lt;/li&gt;
      &lt;li id="5"&gt;Item 5&lt;/li&gt;
      &lt;li id="6"&gt;Item 6&lt;/li&gt;
   &lt;/ul&gt;
&lt;/div&gt;
</code></pre>

<p>The Javascript currently is the following:</p>

<pre><code>$j('#item').focus(function() {

 // Register keypress events
 $j('#item').keydown(function(e) {
   console.log(e.keyCode);
   switch(e.keyCode) { 
     // User pressed "up" arrow
     case 38:
       e.preventDefault();
       navigate('up');
       console.log('up');
       break;
     // User pressed "down" arrow
     case 40:
       e.preventDefault();
       navigate('down');
       console.log('down');
       break;
     // User pressed "enter"
     case 13:
       e.preventDefault();
       if(item_name != '') {      
         $('#item').val(item_name);        
         console.log('item_name = ' + item_name);
        }
     break;
   }
 });

function navigate(direction){
  if($(":visible","#suggestions").length &gt; 0){
  var lis = $j("li", "#suggestions");
  if(direction == "down"){
    var start = lis.eq(0);
  } else {
    var start = lis.filter(":last");
  }     
  var active = $j("li.active:first", "#suggestions");
  if(active.length &gt; 0){
      if(direction == "down"){
          start = active.next();
      } else {
          start = active.prev();
      } 
  }
  lis.removeClass("active");
  start.addClass("active");

  item_name = start.text();

  }
}   

});//focus
</code></pre>

<p>When I load the page and put my mouse on the text input field and start clicking the down arrow the highlighting of each of the items shows as expected.  But as soon as I click off the input field and click any key then go back to the input field a second time and try to navigate through the list it will skip items.  As you can see I have several console.log to show me what is happening.  The logs indicate that when I click on the down or up arrow (one time) it triggers the event (keyDown) as happening multiple times which then makes it appear that it is skipping items.  I can not figure out why it is doing this.  Is there a way I can clear events after the text field becomes "focus" but before the keyDown occurs? Or am I writing something incorrectly?</p>
