How can I take all data-id from the following line of code
HTML:
<div data-id='1' class="userId">
<span> user name-1 </span>
</div>
<div data-id='2' class="userId">
<span>user name-2</span>
</div>
javascript:
var userIds = $('.userId').attr("data-id");
But it returns only 1
How does it work?
it grabs all the
userIdclass elements, then it will execute a function(withmap) that will return thedata-idattribute usingdataand then get the results with `get.Why doesn’t your code work?
attrworks only on the first matched element in the set, so it returns only the first element id-1.attr: Get the value of an attribute for the first element in the set of matched elements…