javascript - How to put all elements' content in array using jQuery ? -


<div id="main"> <p>text1</p> <p>text2</p> <p>text3</p> </di> 

result should :

["text1","text2","text3"] 

jquery provides .map() this:

var items = $('#main p').map(function () { return $(this).text(); }).get(); 

.map() iterates on elements, invoking function on each of them , recording return value of function in new array, returns.

you have solved simple .each():

var items = [];  $('#main p').each(function (i, e) {   items.push($(e).text()); }); 

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -