Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get list of data-foo attributes for set of jQuery elements [duplicate]

Tags:

jquery

each

I can use attr to "Get the value of an attribute for the first element in the set of matched elements ..."

To get the list of attributes, do I need to build it up manually with $(..).each or is there a convenience?

$('tr.food-row')
[
<tr class=​"food-row" data-pk=​"11457">​…​</tr>​
, 
<tr class=​"food-row" data-pk=​"11429">​…​</tr>​
]
$('tr.food-row').attr('data-pk')
"11457"

I want to get ["11457", "11429"].

like image 655
Skylar Saveland Avatar asked Mar 02 '13 22:03

Skylar Saveland


1 Answers

var allAttributes = $('tr.food-row').map(function(){
    return $(this).data('pk');
}).get();

Live DEMO

like image 171
gdoron is supporting Monica Avatar answered Nov 11 '22 04:11

gdoron is supporting Monica