Skip to content

Instantly share code, notes, and snippets.

@first-developer
Created May 17, 2013 14:23
Show Gist options
  • Save first-developer/5599352 to your computer and use it in GitHub Desktop.
Save first-developer/5599352 to your computer and use it in GitHub Desktop.
Live filter : jQuery helper to filter list item by item selector
!(function($) {
$.fn.live_filter = function (list_selector, opts) {
var default_options = {
item_selector : null,
duration : 200 ,
filter: function(el, val){
return $(el).text().toUpperCase().indexOf(val.toUpperCase()) >= 0;
}
}
,options = $.extend(default_options, opts)
,$inputs = $(this)
,$list = $(list_selector)
,$elements = $list.find(options.item_selector)
var filter = default_options.filter
var init = function() {
$inputs.each(function() {
$input = $(this);
$input.keyup(function(){
var val = $input.val();
console.log(this);
var filtered_elements = $elements.filter(function() {
return filter(this, val);
});
var ignored_elements = $elements.not(filtered_elements);
filtered_elements.show(options.duration);
ignored_elements.hide(options.duration);
if (!val) {
$elements.show();
}
});
});
};
init();
return this;
};
$("input[type=text][data-filter=true]").live_filter(".section_list", { item_selector: "li" });
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment