3 ms·
>>allows you to select an item from collected without having to rewrap it with the jquery object I think the following does the same thing: var collected
by albemuth 16y ago
>>allows you to select an item from collected without having to rewrap it with the jquery object
I think the following does the same thing:
var collected = $('a').map(function(element) {return $(element)})
- emehrkay 16y agoNice, I didnt know that jQuery had map. Thanks.
- mwilliamson 16y agoOr even simpler: var collected = $('a').map($) However, map as defined on jQuery objects takes a function that takes the index as the first argument, and the element as the second argument: http://api.jquery.com/map/ http://api.jquery.com/map/ So, what you actually want is: var collected = $('a').map(function(index, element) {return $(element)}) However, jQuery.map has the element as the first argument, and the index as the second argument: http://api.jquery.com/jQuery.map/ http://api.jquery.com/jQuery.map/ I would consider this to be the more intuitive way round, although I'd prefer it if both maps were consistent. So, you could write: var collected = $.map($('a'), $) which strikes me as the neatest version.