Skip to content

Instantly share code, notes, and snippets.

@re5et
Created July 3, 2012 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save re5et/3042846 to your computer and use it in GitHub Desktop.
Save re5et/3042846 to your computer and use it in GitHub Desktop.
jquery little bit lazier map / collect method
(function($){
$.collect = function(){
var selector = arguments[0];
var fn = arguments[1];
var arg = arguments[2];
var collected = [];
$(selector).each(function(){
value = arg ? $(this)[fn](arg) : $(this)[fn]();
collected.push(value);
});
return collected;
};
})(jQuery);
// $.collect('input', 'val');
// $.collect(thing.find('li'), 'data', 'resource-id');
@re5et
Copy link
Author

re5et commented Jul 3, 2012

I don't know why $('input').val() returns the value of the first input matched instead of an array of values from the items it matched.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment