Reputation: 772
I am missing something with the structure of a jQuery object. I need to access the data attribute of an <li>
item.
This produces the expected output, printing the data object of the last <li>
to the console:
console.log($('#assessment_list li:last').data());
But when I try to iterate over the larger object to get the data for each <li>
, I get an error:
for (item in $('#assessment_list li')){
console.log(item.data());
}
throws: "Uncaught TypeError: Object 0 has no method 'data'". How should I alter the selector in the for loop to hit the <li>
elements only and not the other keys in the jQuery object?
Upvotes: 0
Views: 55
Reputation: 39649
You can get an array of all the data values with map
:
var values = $('#assessment_list li').map(function() {
return $(this).data();
}).get();
console.log(values);
Upvotes: -1
Reputation: 87073
Try:
$('#assessment_list li').each(function() {
console.log( $(this).data() );
});
Upvotes: 3