Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <input id="search" />
- <div>
- <div class="child">foo</div>
- <div class="child">bar</div>
- ....
- </div>
- var filterChildren = function() {
- var value = $(this).val().toLowerCase();
- $(".child").filter(function() {
- $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1);
- });
- };
- $("#search").on("keyup", _.debounce(filterChildren, 300));
- $(document).ready(function(){
- var map = {};
- $('.child').each(function(i,el){
- map[$(el).text().toLowerCase()] = $(el);
- });
- $('.child').toggle(false);
- $('#search').on('keyup', function(){
- $('.child').toggle(false);
- var el = map[$(this).val().toLowerCase()];
- if (el)
- $(el).toggle(true);
- });
- });
- $("#search").on("keyup", _.debounce(filterChildren, 300));
- function debounce( fn, threshold ) {
- var timeout;
- return function debounced() {
- if ( timeout ) {
- clearTimeout( timeout );
- }
- function delayed() {
- fn();
- timeout = null;
- }
- timeout = setTimeout( delayed, threshold || 100 );
- };
- }
Add Comment
Please, Sign In to add comment