简体   繁体   中英

Get all values for a specific data attribute

As simple as the title states, i want to get all values for for elements that contain a specific data-attribute . Any solutions I've found have been directly related to querying the value of the attribute which is not what I want.

So for example - if I have multiple elements with the attribute data-help-id how do I retrieve all those attributes either as an array or an object?

I would use .map to transform your jQuery object containing elements with the attribute you want into an array of values:

var $elements = $('[data-help-id]')
    , values = $elements.map(function () {
          return $(this).data('help-id');
      }).get();

Example: http://jsfiddle.net/68kcg/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM