Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
7
rated 0 times [  14] [ 7]  / answers: 1 / hits: 31410  / 12 Years ago, wed, august 1, 2012, 12:00:00

I have a list here



<ul id=demo2 data-name=demo2>
<li data-value=here>here</li>
<li data-value=are>are</li>
<li data-value=some...>some</li>
<!-- notice that this tag is setting a different value :) -->
<li data-value=initial>initial</li>
<li data-value=tags>tags</li>
</ul>


Where each li item has a custom data attribute. On JQuery how would get all of the values of each li element which has an attribute of data-value? I want to get their value.



but this code of mine doesn't seem to be working



        $('#view-tags').click(function(){
$('li[data-value]').each(function(){
alert($(this).data(value));
})
});


The whole code on jsfiddle: http://jsfiddle.net/Zn3JA/


More From » jquery

 Answers
30

You are pretty close. You can use jQuery's .data() method to read attributes that start with data-. So in your case .data(value) since your attribute is data-value=some.



This should do it:



$('li[data-value]').each(function(){
alert($(this).data(value));
});


Here is a working fiddle as well: http://jsfiddle.net/nuphP/


[#83924] Tuesday, July 31, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
emerymariamm

Total Points: 276
Total Questions: 97
Total Answers: 99

Location: Comoros
Member since Sun, Dec 13, 2020
4 Years ago
emerymariamm questions
Fri, Mar 4, 22, 00:00, 2 Years ago
Wed, Jan 12, 22, 00:00, 2 Years ago
Sun, Jul 4, 21, 00:00, 3 Years ago
Wed, Dec 23, 20, 00:00, 4 Years ago
;