Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jun 20th, 2013  |  syntax: None  |  size: 3.32 KB  |  views: 25  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <span>
  2. <checkbox><label></br>
  3. <checkbox><label></br>
  4. <checkbox><label></br>
  5. </span>
  6.        
  7. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="RealtimeCheckBoxListFiltering.Default" %>
  8.  
  9. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  10. <html xmlns="http://www.w3.org/1999/xhtml">
  11. <head runat="server">
  12.   <title></title>
  13.   <script type="text/javascript">
  14.     window.onload = function () {
  15.       var tmr = false;
  16.       var labels = document.getElementById('cblItem').getElementsByTagName('label');
  17.       var func = function () {
  18.         if (tmr)
  19.           clearTimeout(tmr);
  20.         tmr = setTimeout(function () {
  21.           var regx = new RegExp(document.getElementById('inputSearch').value);
  22.           for (var i = 0, size = labels.length; i < size; i++)
  23.             if (document.getElementById('inputSearch').value.length > 0) {
  24.               if (labels[i].textContent.match(regx)) setItemVisibility(i, true);
  25.               else setItemVisibility(i, false);
  26.             }
  27.             else
  28.               setItemVisibility(i, true);
  29.         }, 500);
  30.  
  31.         function setItemVisibility(position, visible)
  32.         {
  33.           if (visible)
  34.           {
  35.             labels[position].style.display = '';
  36.             labels[position].previousSibling.style.display = '';
  37.             if (labels[position].nextSibling != null)
  38.               labels[position].nextSibling.style.display = '';
  39.           }
  40.           else
  41.           {
  42.             labels[position].style.display = 'none';
  43.             labels[position].previousSibling.style.display = 'none';
  44.             if (labels[position].nextSibling != null)
  45.               labels[position].nextSibling.style.display = 'none';
  46.  
  47.           }
  48.         }
  49.       }
  50.  
  51.       if (document.attachEvent) document.getElementById('inputSearch').attachEvent('onkeypress', func);  // IE compatibility
  52.       else document.getElementById('inputSearch').addEventListener('keydown', func, false); // other browsers
  53.     };
  54.   </script>
  55. </head>
  56. <body>
  57.   <form id="form1" runat="server">
  58.   <table>
  59.     <tr>
  60.       <td>
  61.         <asp:TextBox runat="server" ID="inputSearch" ClientIDMode="Static" />
  62.       </td>
  63.     </tr>
  64.     <tr>
  65.       <td>
  66.         <asp:CheckBoxList runat="server" ID="cblItem" ClientIDMode="Static" RepeatLayout="Flow" />
  67.       </td>
  68.     </tr>
  69.   </table>
  70.   </form>
  71. </body>
  72. </html>
  73.        
  74. using System;
  75. using System.Collections.Generic;
  76.  
  77. namespace RealtimeCheckBoxListFiltering
  78. {
  79.     public partial class Default : System.Web.UI.Page
  80.     {
  81.         protected void Page_Load(object sender, EventArgs e)
  82.         {
  83.             cblItem.DataSource = new List<string>() { "qwe", "asd", "zxc", "qaz", "wsx", "edc", "qsc", "esz" };
  84.             cblItem.DataBind();
  85.         }
  86.     }
  87. }
  88.        
  89. var tmr = false;
  90. var labels = document.getElementsByTagName('label')
  91. var func = function() {
  92.     if (tmr) clearTimeout(tmr);
  93.     tmr = setTimeout(function () {
  94.         var regx = new Regex(inputVal); /* Input value here */
  95.         for(var i = 0, size = labels.length; i < size; i++) {
  96.             if(regx.match(labels[i].textContent || labels[i].innerText)) labels[i].style.display = 'block';
  97.             else labels[i].style.display = 'none';
  98.         }
  99.     }, 100);
  100. }
  101. if (document.attachEvent) inputField.attachEvent('onkeypress', func);
  102. else inputField.addEventListener('keypress', func, false);