title: change enter key behavior in jquery autocomplete
body: <p>I'm using jquery's autocomplete widget and I want the enter key to serve two purposes:</p>
  
  <ol>
  <li>If the user hits the enter key when focus is on the search box, it should open a form (regardless of current matches).</li>
  <li>If the system finds a match <strong>and the user selects it</strong> from the list by pressing enter, it should display a different dialog.</li>
  </ol>
  
  <p><strong>The problem is I haven't been able to tell events apart, as it seems that when I choose an option from the autocomplete list of matches, the text box retains focus.</strong></p>
  
  <p>This is where I'm at right now:</p>
  
  <p><strong>My autocomplete function:</strong></p>
  
  <pre><code>jQuery('#nom_id_busqueda').autocomplete({
      source: function(request, response) {
          jQuery.ajax({
              url: myUrl,
              dataType: "json", 
              data: {term: encodeURI(request.term)},
              success: function (answer) {
                 if (answer.length == 0) {
                    jQuery("#encontrada").hide();
                    jQuery("#no_encontrada").show();
                 }
                 else {
                    jQuery("#no_encontrada").hide();
                    response(answer);
                 }
              },
              error : function(){ alert("Something went wrong."); },
          });
      },
      minLength:2,
      delay:400,
      select: function(event, ui) {
          if (ui.item){
              jQuery("#nom_id_busqueda").val(ui.item.label);
              jQuery("#per_busq").val(ui.item.value);
              event.preventDefault();
              jQuery("#encontrada").show();
          }
      },
  });
  </code></pre>
  
  <p><strong>and my event listener for the keystroke:</strong></p>
  
  <pre><code>jQuery('#nom_id_busqueda').keydown(function (e){
      if(e.keyCode == 13){
          if(jQuery("#nom_id_busqueda").is(":focus")) {
              jQuery("#no_encontrada").hide();
              jQuery("#encontrada").hide();
              jQuery("#div_formu_busq").dialog("close");
              jQuery("#div_formu_per").dialog("open");
          }
      }
  });
  </code></pre>
  
  <p>Any ideas? Thanks.</p>
  
  <p>Update:
  Here's my DOM:</p>
  
  <pre><code>&lt;div id="div_formu_busq" class="formu_fs" title="&lt;?php echo JText::_('COM_BUSQUEDA_PERSONA');?&gt;"&gt;
  &lt;form name="formu_busq" onsubmit="return false;"&gt;
      &lt;fieldset&gt;
          &lt;legend&gt;&lt;?php echo JText::_('COM_BUSCAR_EN_EXISTENTES');?&gt;&lt;/legend&gt;
          &lt;?php echo JText::_("COM_ESCRIBA_EL_NOMBRE_O_EL_NUMERO_DE_IDENTIFICACION_PARA_BUSCAR_EN_EL_SISTEMA").' '. JText::_("COM_ENTER_LO_LLEVA_AL_FORMULARIO") ;?&gt;&lt;br&gt;
          &lt;ul&gt;
          &lt;li&gt;
              &lt;label for=nom_id_busqueda&gt;&lt;?php echo JText::_("COM_NOMBRE_O_NUMERO_DE_DOCUMENTO");?&gt;&lt;/label&gt;
              &lt;input type="text" name="nom_id_busqueda" id="nom_id_busqueda" /&gt;
          &lt;/li&gt;
      &lt;/ul&gt;
      &lt;input type="hidden" name="per_busq" id="per_busq" /&gt;
  &lt;/form&gt;
  &lt;br&gt;
  &lt;div id="encontrada" class="buts"&gt;
      &lt;fieldset&gt;
          &lt;p&gt;&lt;?php echo JText::_("COM_SE_ENCONTRO_UNA_PERSONA_CON_EL_MISMO_NOMBRE_Y_O_IDENTIFICACION");?&gt; &lt;a class="button_fs_a blue_b" onclick="irAPersona()" &gt;&lt;?php echo JText::_('COM_IR_A_PERSONA'); ?&gt;&lt;/a&gt;&lt;/p&gt;
      &lt;/fieldset&gt;
  &lt;/div&gt;
  
  &lt;div id="no_encontrada" class="buts"&gt;
      &lt;fieldset&gt; 
          &lt;p&gt;
              &lt;?php echo JText::_("COM_NO_SE_ENCONTRARON_RESULTADOS_SIMILARES");?&gt;
              &lt;a class="button_fs_a blue_b" id="m_formu_per" &gt;&lt;?php echo JText::_('COM_AGREGAR_USUARIO'); ?&gt;&lt;/a&gt;
          &lt;/p&gt;
      &lt;/fieldset&gt; 
  &lt;/div&gt;
  </code></pre>
  
  <p></p>