1

$('#asearch').on('input', function(){
 let a = $(this).val();
 $('.title').hide();
 $('.title:contains(' + a + ')').show();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type = 'search' class='asearch' id='asearch' placeholder='Search'>
<div class='title'>loRem</div>
<div class='title'>IpsuM</div>

I want to get the first title visible if lor is typed inside the search box and the second title if i is typed i.e. want the search box to be case insensitive.

Any help?

qadenza
  • 9,025
  • 18
  • 73
  • 126

2 Answers2

1

You can modify the .contains filter to be case insensitive or create your own selector.

 jQuery.expr[':'].contains = function(a, i, m) {
  return jQuery(a).text().toUpperCase()
      .indexOf(m[3].toUpperCase()) >= 0;
};

jQuery.expr[':'].contains = function(a, i, m) {
  return jQuery(a).text().toUpperCase()
      .indexOf(m[3].toUpperCase()) >= 0;
};


$('#asearch').on('input', function(){
 let a = $(this).val();
 $('.title').hide();
 $('.title:contains(' + a + ')').show();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type = 'search' class='asearch' id='asearch' placeholder='Search'>
<div class='title'>loRem</div>
<div class='title'>IpsuM</div>
Rishab
  • 1,484
  • 2
  • 11
  • 22
1

:contains selector in jquery is case sensitive. You can use regex with filter()

$('#asearch').on('input', function() {
  let a = $(this).val();
  $('.title').hide();
  $('.title').filter(function() {
    return new RegExp(a, 'i').test($(this).text())
  }).show();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type='search' class='asearch' id='asearch' placeholder='Search'>
<div class='title'>loRem</div>
<div class='title'>IpsuM</div>
User863
  • 19,346
  • 2
  • 17
  • 41