My code looks like this:
- Link 1
<
Stumbled into this question and came up with a more reusable answer:
$.fn.collect = function(fn) {
var values = [];
if (typeof fn == 'string') {
var prop = fn;
fn = function() { return this.attr(prop); };
}
$(this).each(function() {
var val = fn.call($(this));
values.push(val);
});
return values;
};
var ids = $('#ulList li').collect('id');
var links = $('#ulList a').collect('href');
You can also pass a function into collect like so:
var widths = $('#ulList li').collect(function() {
return this.width();
});