I have a list of songs, and I want users to be able to filter them by typing into a text field. Here's what I'm doing now:
$("#filter_song_list").keyup(function() {
var filter = new RegExp($(this).val(), "i");
$("ul.song_list a").each(function(){
if (!$(this).text().match(filter)) {
$(this).hide();
} else {
$(this).show();
}
});
});
- Is this the right approach?
- Since I'm interpreting the user's input as a regular expression, he can't search for songs with (say) a period in the title without first escaping it ("."). How can I fix this while still maintaining the case-insensitivity of the search?