1

I would like to get an array of the values from the data-val attributes of .my-li elements

<ul id="stuff">
<li class='my-li' data-val='1'>
<li class='my-li' data-val='2'>
<li class='my-li' data-val='3'>
<li class='my-li' data-val='4'>
<li class='my-li' data-val='5'>
<ul>

here the result should be [1,2,3,4,5];

anybody knows a good way of doing this ?

3
  • That's an HTML class. There is no such thing as a CSS class. (There are class selectors, selectors, rules, rule-sets, and properties that people has mistakenly referred to as classes though) Commented Jan 12, 2012 at 15:13
  • @Quentin look here tizag.com/cssT/class.php "Css Class", also if you google it you'll see lots of "css class" usages, even in asp.net web-forms all controls have a CssClass property Commented Jan 16, 2012 at 22:08
  • — Lots of people using a non-standard term with multiple meanings does not make it a useful term to use. I've come across Tizag a few times and never seen anything that that wasn't dreadful, it might even be worse then W3Schools and that is saying something. Commented Jan 16, 2012 at 23:23

3 Answers 3

2

Try:

var foo = $('#stuff .my-li').map(function () {
  return $(this).data('val');
});
Sign up to request clarification or add additional context in comments.

Comments

2

try this simple one.

var array = [];
$('.my-li').each(function(){
array.push($(this).attr('data-val'));
});
alert(array);

fiddle : http://jsfiddle.net/pp5pw/

Comments

1
var foo = $('#stuff .my-li').map(function () {
   return $(this).attr('data-val');
});

console.log(foo[0])

jsfiddle goes to http://jsfiddle.net/granjoy/KxQAr/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.