2

I have a list on a page and a search bar that searches and filters the list as the user types. At the moment the input has to be all lowercase (which I've added myself). How can I remove all case sensitivity from the search string? For example if I search for teST the result for Test would still appear.

var list = $("table.ms-listviewtable");
var listItems = $("table.ms-listviewtable tr:not(.ms-viewheadertr)"); 
var input = $("input#filterInput");

input.keyup(function() {
  listItems.each(function() {
    var text = $(this).text();
    var text = $(this).text().toLowerCase();
    if (text.indexOf(input.val()) != -1) {
      $(this).show();
    } else {
      $(this).hide();
    }
  });
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Celias
  • 29
  • 1
  • 8
  • 2
    `if (text.indexOf(input.val().toLowerCase()) != -1)` – Pranav C Balan Aug 01 '19 at 09:09
  • 1
    This worked, thanks very much! Will also mark the below as correct. – Celias Aug 01 '19 at 09:15
  • Please cache the input val. Also I suggest to use toggle: `input.on("input",function() { var val = $(this).text().toLowerCase(); listItems.each(function() { var text = $(this).text().toLowerCase(); $(this).toggle(text.indexOf(val)!=-1); }) })` – mplungjan Aug 01 '19 at 09:23

2 Answers2

0

Just use toLowerCase on input.val() as well as text.

if (text.indexOf(input.val().toLowerCase()) != -1) {...}

Also note you can use includes instead of indexOf on newer browsers:

if (text.includes(input.val().toLowerCase())) {...}
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
0

You can also use the .match method

var list = $("table.ms-listviewtable");
var listItems = $("table.ms-listviewtable tr:not(.ms-viewheadertr)"); 
var input = $("input#filterInput");

input.keyup(function() {
  let inputVal = input.val().toLowerCase();
  listItems.each(function() {
    if($(this).text().toLowerCase().match(inputVal)){
      $(this).show();
    } else {
      $(this).hide();
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Rohit.007
  • 3,414
  • 2
  • 21
  • 33