Reputation: 530
I have a list:
<ul class='class-name'>
<li><p>value1</p></li>
<li></li>
<li><p>value2</p></li>
<li><p>value3</p></li>
</ul>
I want to get value1,value2,value3
. I'm using:
$('ul.class-name > li > p').text();
But I'm getting value1value2value3
.
Can anyone tell me how to get a comma separated value?
Upvotes: 9
Views: 3366
Reputation: 490489
You could try this...
$('ul.class-name > li > p')
.map(function() { return $(this).text(); }).get().join();
This gets all the p
elements, iterates over them replacing their references with their text, then gets a real array from the jQuery object, and joins them with join()
(the ,
is the default separator).
Upvotes: 15