line1
line 2With jQuery, how can...">Geek asks and answersPut all element values ββin an arrayI 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?+3javascript jqueryamateur Mar 13 '11 at 1:42source share2 answersUse .map():var arr = $('.class1 p').map(function () { return $(this).text(); }).get(); +7David tang Mar 13 '11 at 1:44source shareI 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(); }); +2Lightness races in orbit Mar 13 '11 at 1:48source shareMore articles:PHP OOP Good practice for accessing methods? - methodsChanging the behavior of carriage position movement in intellij - intellij-ideaHighlight composite identifier value for use in business logic? - c #How does a computer work if the value is greater? - language-agnosticPass all parameters forward? - ruby-on-railsJava EE, EJBs File Processing - java-eeproblem using getElementById (). getValue on FBJS / Facebook! - javascriptQuestion about JUNG assignment - javaconnect to remote oracle database through visual studio 2010 - oracleHow to get visual horizontal position (X axis) of WPF DataGridCell? - wpfAll Articles
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?
Use .map():
.map()
var arr = $('.class1 p').map(function () { return $(this).text(); }).get();
I assume that you mean the contents of the elements <p>, not their meaning (this is nothing).
<p>
var text = []; $('.class1 > p').each(function() { text[text.length] = $(this).text(); });