Using jQuery to select a list of values

I have a long list of items with a data-item attribute; e.g., something like:

<div class="store-event" data-event="12345"> ... </div>

I would like to collect all the data-event values into an array (jQuery or otherwise) and do that in a single line so it’s easy to enter in the developer console but I haven’t been able to figure that out. $(".store-event") gets me the expected jQuery collection, but adding .data("event") only gets me the first value.

Any slick ways to make this happen without having to iterate over the original collection?

Take a look at .map()

In your case would be something like (not tested):

$(".store-event" ).map(function() { return $(this).data("event") }).get()

Cheers!

1 Like

Perfect, @lompy - worked like a champ; thanks! For some reason, I have tendency to overlook the map function in this and other languages: gotta address that.