line1

line 2With jQuery, how can...">

Put all element values ​​in an array

I have the following markup:

<div class="class1"><p>line1</p><p>line 2</p></div>

With jQuery, how can I take the values ​​of all p tags in a div and put them in an array?

+3
source share
2 answers

Use .map():

var arr = $('.class1 p').map(function () {
    return $(this).text();
}).get();
+7
source

I assume that you mean the contents of the elements <p>, not their meaning (this is nothing).

var text = [];
$('.class1 > p').each(function() {
    text[text.length] = $(this).text();
});
+2
source

All Articles