Filter list items with jQuery

Sometimes while working with list items or contents, we need to filter out the list items or contents for some matching records. We can implement this using jQuery with ease. Here is a little jQuery code snippet to implement local search.

  <script src=""> </script>
  <script type="text/javascript">
  $(document).ready(function(e) {
    var val = $(this).val();
      ($(this).text().search(new RegExp(val, "i")) === -1) 
             ? $(this).hide() : $(this).show();

  <input type="text" id="search" />
  <ul class="list">
    <li>New Zealand</li>
    <li>South Africa</li>
    <li>Sri Lanka</li>
    <li>West Indies</li>

Here is a working demo

  • Australia
  • Bangladesh
  • India
  • New Zealand
  • Pakistan
  • South Africa
  • Sri Lanka
  • West Indies

Filter list items with jQuery Filter list items with jQuery Reviewed by Altaf Hussain on June 07, 2015 Rating: 5

No comments:

Altaf Web. Powered by Blogger.