1

I'm using the script below to filter though results in a table. Only the problem is that it is case sensitive. How would I go about making it non-case sensitive?

<script>
 $(document).ready(function() {

      $("#searchInput").keyup(function(){
    //hide all the rows
          $("#fbody").find("tr").hide();

    //split the current value of searchInput
          var data = this.value.split(" ");
    //create a jquery object of the rows
          var jo = $("#fbody").find("tr");

    //Recusively filter the jquery object to get results.
          $.each(data, function(i, v){
              jo = jo.filter("*:contains('"+v+"')");
          });
        //show the rows that match.
          jo.show();
     //Removes the placeholder text  

      }).focus(function(){
          this.value="";
          $(this).css({"color":"black"});
          $(this).unbind('focus');
      }).css({"color":"#C0C0C0"});

  });

</script>
Damon
  • 3,004
  • 7
  • 24
  • 28
user3078580
  • 67
  • 1
  • 11
  • 1
    possible duplicate of [How do I make jQuery Contains case insensitive, including jQuery 1.8+?](http://stackoverflow.com/questions/2196641/how-do-i-make-jquery-contains-case-insensitive-including-jquery-1-8) – j08691 Dec 09 '13 at 21:27
  • A good way to make it case-insensitive is to apply `toLowerCase()` to both, so `var foo = 'HelLO WoRLd!'`, `var boo = 'HELLO world!'` so `foo.toLowerCase() === boo.toLowerCase()` – Nico Dec 09 '13 at 21:37

4 Answers4

1

http://jsfiddle.net/baeXs/ and http://css-tricks.com/snippets/jquery/make-jquery-contains-case-insensitive/ will help you out

$.expr[":"].containsNoCase = $.expr.createPseudo(function(arg) {
    return function( elem ) {
        return $(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
    };
});
Gordon Tucker
  • 6,653
  • 3
  • 27
  • 41
1

Can do somthing like this with filter():

 $.each(data, function (i, v) {
     v = v.toLowerCase();    
     jo.filter(function () {
         var txt = $(this).text().toLowerCase();
         return txt.indexOf(v) > -1;
     }).show();    
 })
charlietfl
  • 170,828
  • 13
  • 121
  • 150
0
$(document).ready(function() {

  $("#searchInput").keyup(function(){
//hide all the rows
      $("#fbody").find("tr").hide();

//split the current value of searchInput
      var data = this.value.toLowerCase().split(" ");
//create a jquery object of the rows
      var jo = $("#fbody").find("tr");

//Recusively filter the jquery object to get results.
      $.each(data, function(i, v){
          jo = jo.filter("*:contains('"+v.toLowerCase()+"')");
      });
    //show the rows that match.
      jo.show();
 //Removes the placeholder text  

  }).focus(function(){
      this.value="";
      $(this).css({"color":"black"});
      $(this).unbind('focus');
  }).css({"color":"#C0C0C0"});

});
kei
  • 20,157
  • 2
  • 35
  • 62
0

I'd probably change the filter:

      $.each(data, function(i, v) {
          jo = jo.filter(function() {
            return $(this).text().toLowerCase().indexOf(v.toLowerCase()) > -1;
          });
      });
Pointy
  • 405,095
  • 59
  • 585
  • 614