Skip to content

Instantly share code, notes, and snippets.

@spiralx
Created September 10, 2013 16:18
Show Gist options
  • Save spiralx/6511813 to your computer and use it in GitHub Desktop.
Save spiralx/6511813 to your computer and use it in GitHub Desktop.
Creates jQuery instance methods for returning data from each item selected e.g. $el.attrs(), $.texts()
(function($) {
$.fn.batch = function(method, args) {
var func = $.fn[method], results = [];
this.each(function() {
results.push(func.apply(this, args));
});
return results;
};
var funcs = "attr css=styles prop html text val offset width height".split(" ");
$.each(funcs, function(i, v) {
var names = v.indexOf("=") == -1 ? [v, v + "s"] : v.split("="),
cur_method = names[0], batch_method = names[1];
if ($.fn[cur_method] && !$.fn[batch_method]) {
$.fn[batch_method] = function() {
return this.batch(cur_method, arguments);
}
}
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment