Badr B
Badr B

Reputation: 57

How to get a multiple data attribute with jquery

I need your help in this topic i have the html code :

<ul data-role="rows">
<li data-name="member">member X</li>
<li data-name="question">question X</li>
</ul>
<ul data-role="other">
<li data-name="oth">...</li>
...
</ul>

I need to get data-name values where data-role = rows which means i need to get these values : "member" and "question"

thank you for your help

Upvotes: 1

Views: 1851

Answers (1)

adeneo
adeneo

Reputation: 318162

Just target those elements, and map back the data attributes

var names = $('[data-role=rows] [data-name]').map(function(_,el){
    return $(el).data('name');
}).get();

FIDDLE

Upvotes: 2

Related Questions