Search code examples
javascriptsearchcase-insensitive

javascript search - case-insensitive


I'm having troubles with getting this code to be case-insensitive. This code is found online - (http://jsfiddle.net/bizamajig/radzq2o2/)

 $(function(){
    // Instantiate MixItUp:
    $container = $('.cd-gallery')
    $container.mixItUp();
    $('#filter').keyup(function(){
      var val = $(this).val();
      var state = $container.mixItUp('getState');
      var $filtered = state.$targets.filter(function(index, element){
        return $(this).text().toString().indexOf( val.trim() ) >= 0;
      });

      $container.mixItUp('filter', $filtered);
    });
  });

But I can't seem to get this code to be case-insensitive. I've tried different things - but nothing worked. Can anyone help me?

I need this code (above) to be case-insensitive.

Thank you!


Solution

  • Just change

    return $(this).text().toString().indexOf( val.trim() ) >= 0;
    

    to

    return $(this).text().toString().toLowerCase().indexOf( val.trim().toLowerCase() ) >= 0;
    

    Notice the use of toLowerCase(). We basically convert both the strings being compared to lower case letters before comparing them.

    A fork of your fiddle, with the suggested change: http://jsfiddle.net/kykuwppL/