Skip to content

Instantly share code, notes, and snippets.

@oranj
Created September 21, 2011 15:20
Show Gist options
  • Save oranj/1232342 to your computer and use it in GitHub Desktop.
Save oranj/1232342 to your computer and use it in GitHub Desktop.
Filter through a select's options with an input (case insensitive)
$.extend($.expr[":"], {
"containsNC": function(elem, i, match, array) {
return (elem.textContent || elem.innerText || "").toLowerCase
().indexOf((match[3] || "").toLowerCase()) >= 0;
}
});
$(function() {
$('[filterselect]').each(function() {
$(this).change(function() {
if ($(this).val()) {
$('select[name="'+$(this).attr('filterselect')+'"] option').css('display', 'none');
$('select[name="'+$(this).attr('filterselect')+'"] option:containsNC("'+$(this).val()+'")').css('display', 'block');
} else {
$('select[name="'+$(this).attr('filterselect')+'"] option').css('display', 'block');
}
});
});
});
// <input type="text" filterselect="select_name" />
// <select name="select_name"><option>Foo</option><option>Bar</option><option>Baz</option></select>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment