views:

47

answers:

1

i have ul like this

  <ul class="options" id="selt_count">
  <li value="1">One</li>
  <li value="2">Two</li>
  <li value="5">Five</li>
  <li value="12">Other</li>
  </ul>

what i want is:

get all values of of li into variable in following format (1,2,5,12)

in jquery

Thanks

+3  A: 

How about:

var values = $('#selt_count li').map(function() {
    return this.value
});
values; // [1, 2, 5, 12]

To get a string representation you can do:

var s = '(' + values.get().join(',') + ')'; // "(1,2,5,12)"

FYI the value attribute was deprecated a while ago.

References:

Roatin Marth
Note that this doesn't return a true array, it's only an array-like object. You'll probably want to call get() to make it a real array. Or use the utility $.map() method, which will always return an array.
J-P
@J-P: see edit.
Roatin Marth