Created
July 3, 2012 20:29
-
-
Save re5et/3042846 to your computer and use it in GitHub Desktop.
jquery little bit lazier map / collect method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.