M. Gar
M. Gar

Reputation: 887

Search text no matter if is in lower or upper case

I have a function that I use to search content in my web using one text box <input class="form-control" id="search" type="text" placeholder="Search" /> the showing content are in different panels, so this is my script.

$('#search').keyup(function () {
    var term = $(this).val();
    if (term != '') {
        $('.panel').hide();
        $('.panel').filter(function () {
            return $(this).text().indexOf(term) > -1
        }).show();
    } else {
        $('.panel').show();
    }
});

That works fine but only with the exactly match, if I write in the text box Hello shows me only the Hello words but I need that shows me to hello, hEllO or HELLO, all the string no matter the lower or upper case.

Any help is really appreciated, sorry for my bad grammar.

Upvotes: 1

Views: 11559

Answers (3)

paranjothi
paranjothi

Reputation: 113

use .toUpperCase() both your input val and filter val

Upvotes: -1

Sami
Sami

Reputation: 3800

Try to use toLowerCase() function to by pass case-sensitive matching, like:

$(this).text().toLowerCase().indexOf(term.toLowerCase()) > -1

Upvotes: 5

TimoStaudinger
TimoStaudinger

Reputation: 42460

Convert both the search string and the text you are searching in to lower case:

return $(this).text().toLowerCase().indexOf(term.toLowerCase()) > -1

More info on MDN

Upvotes: 6

Related Questions