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?
children() is an inbuilt method in jQuery which is used to find all the children element related to that selected element. This children() method in jQuery traverse down to a single level of the selected element and return all elements. Syntax: $(selector).children() Here selector is the selected element whose children are going to be found.
For instance, you can use jQuery n th child selectors which can count children from the last to first, select jQuery first child, or do other tasks that depend on sibling relations. jQuery .children () method traverses downwards a single level of the DOM tree and looks for descendants of specified elements.
Last Updated : 13 Feb, 2019 children () is an inbuilt method in jQuery which is used to find all the children element related to that selected element. This children () method in jQuery traverse down to a single level of the selected element and return all elements.
When traversing downwards, jQuery .children () returns all direct children and traverse one level down the DOM tree. Here is a code example to illustrate how this method is used: The method returns the direct children of the element you select, and uses syntax like this:
You could try this...
$('ul.class-name > li > p')
.map(function() { return $(this).text(); }).get().join();
jsFiddle.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With