title: improving performance of table navigation in jquery
body: <p>I am trying to write table navigation using keys for asp.net gridview. This is what I have so far. It is working fine but it is slow. Please look at it and let me know how to improve the performance if possible.</p>
  
  <p>Basically it is html table with input elements. The structure of table is like this</p>
  
  <pre><code>&lt;table&gt;
  &lt;tr&gt;
  &lt;td&gt;
  &lt;div style="height:22px;"&gt;
       &lt;input type = "text"&gt;
  &lt;/div&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;
  &lt;div style="height:22px;"&gt;
       &lt;input type = "text"&gt;
  &lt;/div&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  ...
  &lt;/table&gt;
  jQuery(function($) {
               $('table#&lt;%= myTable.ClientID %&gt;')
                  .bind('keydown', funcKeyDown)
          });
  
  
  function funcKeyDown(event)
          {   
              //get cell element.
              var cell = event.target;
  
              //get current cellIndex
              var $cell = $(cell);         
              var currCell = $cell.parents("td"); 
              var cellIndex = currCell[0].cellIndex;
              //get current rowIndex
              var currRow = $cell.parents("tr");
              var rowIndex = currRow[0].rowIndex;   
  
              var nextRowIndex, targetElem = null; 
              var nextCell;           
              switch(event.keyCode) {                
                  case 13: //enter key
                      if(shiftKeyPressed == 1){
                          //move left                        
                          if(!(cellIndex == 0)){                        
                          targetElem = currRow.children("td").eq(cellIndex-1).find("input[type=text]");
                          if(targetElem){targetElem.select();}}}
                      else {
                          //move right
                         if(!(cellIndex == (numElements -1))){
                              targetElem = currRow.children("td").eq(cellIndex+1).find("input[type=text]");                        
                              if(targetElem){
                                  targetElem.select();
                              }
                          }
                      }                         
                      return false;                          
                  case 16: //shift key
                      shiftKeyPressed = 1;
                      return false;
                  case 33:                    
                  case 34: //page-up, page-down
                      if(event.keyCode == 33){
                          nextRowIndex = rowIndex - pageSize;
                          if(nextRowIndex &lt; 0) nextRowIndex = 0;                
                      }
                      if(event.keyCode == 34){
                          nextRowIndex = rowIndex + pageSize;
                          if(nextRowIndex &gt; numRows) nextRowIndex = numRows;
                      }                      
                      targetElem = currRow.parent().children("tr").eq(nextRowIndex).children("td").eq(cellIndex)
                                      .find("input[type=text]");                                                                
                      if(targetElem != null){targetElem.select();}
                      return false;                   
                  case 37: //left       
                      if(!(cellIndex == 0)){                        
                          targetElem = currRow.children("td").eq(cellIndex-1).find("input[type=text]");
                          if(targetElem != null){targetElem.select();}}
                      return false;                                                
                  case 38: //up                    
                      if(rowIndex != 0){
                          targetElem = currRow.parent().children("tr").eq(rowIndex-1).children("td").eq(cellIndex)
                                          .find("input[type=text]");                                                                
                          if(targetElem != null){
                          targetElem.select();}}
                      return false;                                                
                  case 39: //right arrow
                      if(!(cellIndex == (numElements -1))){
                          targetElem = currRow.children("td").eq(cellIndex+1).find("input[type=text]");                        
                          if(targetElem != null){targetElem.select();}}
                      return false;                                                
                  case 40: //down                                               
                      if(rowIndex &gt;= 0){
                          targetElem = currRow.parent().children("tr").eq(rowIndex+1).children("td").eq(cellIndex)
                                          .find("input[type=text]");                                                                
                          if(targetElem != null){targetElem.select();}}
                      return false;                                                                                                        
              }            
          }                
  </code></pre>