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
Post a Comment