If I have the following HTML:
<ul> <li>List 1</li> <li>list 2</li> <li>list 3</li> </ul>
Can I get the text content from the the <li>'s and place them in array using javascript?
<li>
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).
map()
The get()(docs) method (when passed no argument) converts that jQuery object into an actual Array.
get()
1.4m articles
1.4m replys
5 comments
57.0k users