Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. /* textFilter - a simple module for a text input filter.
  2. * Author -- Daniel Bruckner 2011
  3. *
  4. * To use:
  5. * 1. Include this file on your page, probably at the bottom.
  6. * 2. Add the class "textFilter-input" to any text input tag you would like to
  7. * act as a filter.
  8. * 3. Add the class "textFilter-target" to any DOM element to be hidden if it
  9. * does not match the filter text.
  10. * 4. Add the class "textFilter-match" to the DOM element (contained by a
  11. * textFilter-target) that contains the match text for its ancestor.
  12. *
  13. *
  14. * EXAMPLE:
  15. * <style type="text/css">
  16. * .search-input {
  17. * margin-bottom: 12px;
  18. * }
  19. * .textFilter-target {
  20. * line-height: 10px;
  21. * padding: 10px 4px;
  22. * border-bottom: 1px solid black;
  23. * }
  24. * </style>
  25. *
  26. * <div class="container">
  27. * <div class="search-input">
  28. * <input class="textFilter-input" placeholder="Start typing to filter" />
  29. * </div>
  30. * <div class="textFilter-target">
  31. * <span class="textFilter-match">This is a text sample with unique string "Lorem"</span>
  32. * </div>
  33. * <div class="textFilter-target">
  34. * <span class="textFilter-match">This is a text sample with unique string "Ipsum"</span>
  35. * </div>
  36. * <div class="textFilter-target">
  37. * <span class="textFilter-match">This is a text sample with unique string "Dolor"</span>
  38. * </div>
  39. * <div class="textFilter-target">
  40. * <span class="textFilter-match">This is a text sample with unique string "Sit"</span>
  41. * </div>
  42. * <div class="textFilter-target">
  43. * <span class="textFilter-match">This is a text sample with unique string "Amet"</span>
  44. * </div>
  45. * </div>
  46. *
  47. */
  48. (function () {
  49.  
  50. /* Initialize filter inputs */
  51. var defaultText = $('.textFilter-input').val();
  52.  
  53. $('.textFilter-input')
  54. .focus(function (e) {
  55. if ($(this).val() === defaultText)
  56. $(this).val('');
  57. })
  58. .blur(function (e) {
  59. if ($(this).val() === '')
  60. $(this).val(defaultText);
  61. })
  62. .keyup(function (e) {
  63. var patterns = $(this).val().toLowerCase().split(' ');
  64. if (!patterns.length)
  65. return;
  66. $('.textFilter-target')
  67. .hide()
  68. .filter(function () {
  69. var matchText = $(this)
  70. .find('.textFilter-match')
  71. .text()
  72. .toLowerCase();
  73. for (var i=0; i<patterns.length; i++)
  74. if (matchText.indexOf(patterns[i]) === -1)
  75. return false;
  76. return true;
  77. })
  78. .show();
  79. });
  80.  
  81. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement