Guest User

Untitled

a guest
Aug 15th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. <input id="search" />
  2.  
  3. <div>
  4. <div class="child">foo</div>
  5. <div class="child">bar</div>
  6. ....
  7. </div>
  8.  
  9. var filterChildren = function() {
  10. var value = $(this).val().toLowerCase();
  11. $(".child").filter(function() {
  12. $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1);
  13. });
  14. };
  15.  
  16. $("#search").on("keyup", _.debounce(filterChildren, 300));
  17.  
  18. $(document).ready(function(){
  19. var map = {};
  20. $('.child').each(function(i,el){
  21. map[$(el).text().toLowerCase()] = $(el);
  22. });
  23. $('.child').toggle(false);
  24. $('#search').on('keyup', function(){
  25. $('.child').toggle(false);
  26. var el = map[$(this).val().toLowerCase()];
  27. if (el)
  28. $(el).toggle(true);
  29. });
  30. });
  31.  
  32. $("#search").on("keyup", _.debounce(filterChildren, 300));
  33.  
  34. function debounce( fn, threshold ) {
  35. var timeout;
  36. return function debounced() {
  37. if ( timeout ) {
  38. clearTimeout( timeout );
  39. }
  40. function delayed() {
  41. fn();
  42. timeout = null;
  43. }
  44. timeout = setTimeout( delayed, threshold || 100 );
  45. };
  46. }
Add Comment
Please, Sign In to add comment