I have a text box and a list box, my ultimate aim is to use the text box to filter the list box. The below code works however the text box is case sensitive and I need the user to be able to input any case and it filters the list box accordingly, at moment.
Example
"MY SITE" = Works
"my site" = Works
"My Site" = Does not work
"mY sITE" = Does not work
my JQuery code is
var ddlText, ddlValue, ddl, lblMesg;
$(document).ready(function CacheItems() {
ddlText = new Array(); ddlValue = new Array();
ddl = document.getElementById("<%=lstSites.ClientID %>");
for (var i = 0;
i < ddl.options.length; i++) {
ddlText[ddlText.length] = ddl.options[i].text;
ddlValue[ddlValue.length] = ddl.options[i].value;
}
});
window.onload = CacheItems;
function FilterItems(value) {
ddl.options.length = 0; for (var i = 0; i < ddlText.length; i++) {
if (ddlText[i].toLowerCase().indexOf(value) != -1) {
AddItem(ddlText[i], ddlValue[i]);
}
else if (ddlText[i].toUpperCase().indexOf(value) != -1) {
AddItem(ddlText[i], ddlValue[i]);
}
}
}
function AddItem(text, value) {
var opt = document.createElement("option");
opt.text = text;
opt.value = value;
ddl.options.add(opt);
}
Thanks in advance for any help
You can extend jQuery with a new expression for this.
See contains() and Is there a case insensitive jQuery :contains selector?
…or simply transform both values to upper/lower case;