title: Select ListItem in ListBox based on Text from TextBox
body: <p>I am trying to select an item in a ListBox based on text entered in a textbox using jquery.  If the length of the text entered in the textbox is greater than 1 I would like to loop through the items in the ListBox and compare the value of each item and if it matches the numbers entered in the textbox I need to select/highlight it in the ListBox.  Here is what I am doing but doesn't seem to work.  The ListBox ListItem's are populated at runtime from the database.</p>
  
  <p>Asp.Net</p>
  
  <pre><code>&lt;asp:TextBox ID="txtMediaCode" runat="server" MaxLength="2" Width="40px" /&gt;
  &lt;asp:ListBox ID="lsMediaCodes" runat="server" Width="296px" /&gt;
  </code></pre>
  
  <p>Jquery</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  
      $(document).ready(function () {
  
          $('#txtMediaCode').keyup(function () {
              if ($('#txtMediaCode').length &gt; 1) {
                  $('#lsMediaCodes').each(function (i, option) {
  
                      if ($(option).val() == $('#txtMediaCode').val()) {
                          $(option).attr('selected', 'selected');
                      }
  
                  });
              }
          });
  
      });
  
  &lt;/script&gt;
  </code></pre>
  
  <p>Does anyone have any suggestions?</p>