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 the p tags within the div and place them in an array?
Use .map():
var arr = $('.class1 p').map(function () {
return $(this).text();
}).get();
.get(), the array is jQuery-wrapped, which still works, but you have meaningless functions like find, click, etc, still attached..map() returns a jQuery object and .get() creates a native array from its elements.I will assume you mean the contents of the <p> elements, not their value (which is nothing).
var text = [];
$('.class1 > p').each(function() {
text[text.length] = $(this).text();
});