If I have the following HTML:
- List 1
- list 2
- list 3
var x = [];
$("ul li").each(function() {
x.push($(this).text());
});
or simply:
var x = $.map($("ul li"), function( i ) { return $(i).text(); });
var arr = $("li").map(function() { return $(this).text() }).get();
The map()(docs) method creates a jQuery object populated with whatever is returned from the function (in this case, the text content of each <li>
element).
The get()(docs) method (when passed no argument) converts that jQuery object into an actual Array.