Skip to content

Instantly share code, notes, and snippets.

@brianjlandau
Created May 11, 2010 01:04
Show Gist options
  • Select an option

  • Save brianjlandau/396775 to your computer and use it in GitHub Desktop.

Select an option

Save brianjlandau/396775 to your computer and use it in GitHub Desktop.
// Based on code found at: http://ejohn.org/blog/jquery-livesearch/
(function($) {
$.fn.liveUpdate = function(list, options){
var list = $(list),
original = list.html();
options = $.extend({}, options);
if ( list.length ) {
var rows = list.children('li'),
cache = rows.map(function(){
return $.trim($(this).text().toLowerCase());
});
this.keyup(filter).keyup();
}
return this;
function filter(){
var term = $.trim( $(this).val().toLowerCase() ), scores = [];
if ( !term ) {
list.html(original);
rows = list.children('li');
rows.show();
cache = rows.map(function(){
return $(this).text().toLowerCase();
});
if (options.onReset && $.isFunction(options.onReset)){
options.onReset(list);
}
} else {
rows.hide();
cache.each(function(i){
var score = this.score(term);
if (score > 0) { scores.push([score, i]); }
});
$.each(scores.sort(function(a, b){return b[0] - a[0];}), function(){
$(rows[ this[1] ]).show();
});
}
$(document).trigger('afterFilter.liveUpdate');
}
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment