简体   繁体   中英

Case insensitive jQuery search filter

I have had some help on a Jquery script which creates a searchable filter, The code can be seen here:

$('#search-keyword').on( "keyup", function(){
    if($(this).val()){
        var input = $(this).val();
        $(".filter").hide();
        $("div[data-destination*='"+ input +"']").show();
        if(!$('.filter:visible').get(0)){
            $(".filter").show();
        }
    }else{
        $(".filter").show();
    }
});

The trouble is, if there is the word “How” with an upper case “H” and I search “h”, it wont find it. How can I make this script case insensitive?

Replace this:

$(".filter").hide();
$("div[data-destination*='"+ input +"']").show();

with this:

$(".filter div[data-destination]").hide(); // You have to hide the elements (the divs you want to filter) not the container.
$(".filter div[data-destination]").filter(function() {
    return $(this).data("destination").toLowerCase().indexOf(input.toLowerCase()) !== -1;
}.show();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM