0

I am searching in list of 500 li's. using following code. but facing two problems. one when i press backspace very fastly after typing something, it is not captured. and also searching is case sensitive which i dont want. please suggest improvements in below code :

$('#find_question').bind("keyup", function() {
    searchWord = $(this).val();
    console.log("input length",searchWord);
    if (searchWord.length >= 0) {

        $('#leftSection li').each(function(i, data) {
            text = $(this).text();
            if (text.match(RegExp(searchWord, 'i')))
                $(this).show();

            else
                $(this).hide();
        });
    }
});
user2696466
  • 650
  • 1
  • 14
  • 33

1 Answers1

1

Try this

The containsIgnoreCase comes from How do I make jQuery Contains case insensitive, including jQuery 1.8+?

Live Demo

 $.expr[':'].containsIgnoreCase = function (n, i, m) {
    return jQuery(n).text().toUpperCase().indexOf(m[3].toUpperCase()) >= 0;
 };

$function() {
  $('#find_question').on("keyup", function() {
    searchWord = $(this).val();
    $('#leftSection li').hide();
    if (searchWord.length >= 0) {
      $('#leftSection li:containsIgnoreCase("'+searchWord+'")').show();
    }
  });
});
Community
  • 1
  • 1
mplungjan
  • 169,008
  • 28
  • 173
  • 236