I need a search function inside a checklist box. The idea is that when a user types text into a text field, if that particular item exists in the checkbox list, it should automatically be selected.
HTML:
<asp:TextBox ID="TextBox1" runat="server" OnTextChanged= "return SearchList();"/>
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem>Vincent</asp:ListItem>
<asp:ListItem>Jennifer</asp:ListItem>
<asp:ListItem>Shynne</asp:ListItem>
<asp:ListItem>Christian</asp:ListItem>
<asp:ListItem>Helen</asp:ListItem>
<asp:ListItem>Vladi</asp:ListItem>
<asp:ListItem>Vinz</asp:ListItem>
<asp:ListItem>Churchill</asp:ListItem>
<asp:ListItem>Rod</asp:ListItem>
<asp:ListItem>Mark</asp:ListItem>
</asp:CheckBoxList>
JavaScript:
function SearchList() {
try {
var l = document.getElementById('<%= CheckBoxList1.ClientID %>');
var tb = document.getElementById('<%= TextBox1.ClientID %>');
var p = l.item.length
if (tb.value == "") {
ClearSelection(l);
} else {
for (var i = 0; i < l.options.length; i++) {
if (l.options[i].value.toLowerCase().match(tb.value.toLowerCase())) {
l.options[i].selected = true;
return false;
} else {
ClearSelection(l);
}
}
}
} catch (e) {}
}
function ClearSelection(lb) {
lb.selectedIndex = -1;
}