Case Insensitive - Jquery Search System

-1

Good morning!

I have this code that does the search within a <ul> <li> list:

    $(function(){   

    $('input[type="text"]').keyup(function(){       

        var searchText = $(this).val();

        $('.filter-task > li').each(function(){

            var currentLiText = $(this).text(),
                showCurrentLi = currentLiText.indexOf(searchText) !== -1;

            $(this).toggle(showCurrentLi);

        });

    });

});

I'm trying to get the case sensitive from jquery in the search, doing a search by google I found this code:

    $.expr[":"].contains = $.expr.createPseudo(function(arg) {
    return function( elem ) {
        return $(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
    };
});

But I do not quite understand it, does anyone have something clearer on the subject, or do you have any other way to get the case sensitive?

    
asked by anonymous 02.09.2016 / 15:05

2 answers

1

For the search to be case-insensitive, transform the searched term and the text of the element into UPPERCASE or lowercase

$(function(){   

$('input[type="text"]').keyup(function(){       

    var searchText = $(this).val().toUpperCase();

    $('.filter-task > li').each(function(){

        var currentLiText = $(this).text().toUpperCase(),
            showCurrentLi = currentLiText.indexOf(searchText) !== -1;

        $(this).toggle(showCurrentLi);

    });

});

});
    
02.09.2016 / 15:21
2

You do not need to use any function like this, javascript has a native function to leave everything in the box toUpperCase()

$(function(){   

    $('input[type="text"]').keyup(function(){       

        var searchText = $(this).val().toUpperCase();

        $('.filter-task > li').each(function(){

            var currentLiText = $(this).text().toUpperCase(),
                showCurrentLi = currentLiText.indexOf(searchText) !== -1;
      
            $(this).toggle(showCurrentLi);

        });

    });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputtype="text" >
<ul class="filter-task">
  <li>CACHORRO</li>
  <li>gato</li>
  <li>papagaio</li>
<ul>
    
02.09.2016 / 15:25