Jul 6, 2010

Filter ListBox using javascript

<head id="Head1" runat="server">
  <title>Demotitle>
head>
<script type="text/javascript" language="javascript">
  var myVals=new Array();
   function CacheValues()
   {
   var l =  document.getElementById('ListBox1');
 
   for (var i=0; i < l.options.length; i++)
    {
      myVals[i] = l.options[i].text;
    }
   }
 
  function SearchList()
  {
    var l document.getElementById('ListBox1');
    var tb = document.getElementById('TextBox1');
   
    l.options.length=0;
 
    if(tb.value == "")
    {
      for (var i=0; i < myVals.length; i++)
      {
        l.options[l.options.length] = new Option(myVals[i]);
      }
    }
    else{
 
      for (var i=0; i
      {
        if (myVals[i].toLowerCase().indexOf(tb.value.toLowerCase()) != -1)
        {
          l.options[l.options.length] = new Option(myVals[i]);
        }
        else
        {
          // do nothing
        }
      }
    }
  }
  function ClearSelection(lb)
  {
    lb.selectedIndex = -1;
  }
 
<body onload=CacheValues();>
  <form id="form1" runat="server">
  <input type="text" ID="TextBox1" onkeyup="return SearchList();"/><br />
  <select ID="ListBox1" Height="150px" Width="250px"  size="13" multiple="multiple">
  <option>Vincentoption>
  <option>Jenniferoption>
  <option>Shynneoption>
  <option>Christianoption>
  <option>Helenoption>
  <option>Vladioption>
  <option>Beeoption>
  <option>Jeromeoption>
  <option>Vinzoption>
  <option>Churchilloption>
  <option>Rodoption>
  <option>Markoption>
  select>
  form>
body>
html>
 

No comments:

Post a Comment