0

I have a function for searching. But the problem is that I have to type in the exact word with uppercase and lowercase for the result. I want to change the function so that it doesn't matter the upper lowercase. Here is the function:

$("#search-chats").keyup(function(event) {
        var chatls = $(".chat-list").find('ul');
        var uname  = $(this).val();
        var found  = new Array();

        if (uname.length > 1) {
            chatls.find('span.username').each(function(index, el) {
                var username = $(el).text();
                if (username.indexOf(uname) == -1) {
                    $(el).closest('li').addClass('hidden');
                }
            });
        }
        else{
            chatls.find('li').removeClass('hidden');
        }
    });
Edric
  • 24,639
  • 13
  • 81
  • 91
Webcp
  • 43
  • 3

1 Answers1

1

If you convert both strings to lower case the comparison will be case insensitive. Change this line:

if (username.indexOf(uname) == -1) {

To this line:

if (username.toLowerCase().indexOf(uname.toLowerCase()) == -1) {
Andrew Arthur
  • 1,563
  • 2
  • 11
  • 17