M. Gar M. Gar - 6 months ago 11
Javascript Question

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.

Answer

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