Using .map()

var liValues = $("li").map(function (idx){   return $(this).html(); }).get();

The .map(function) method also iterates through each element in the jQuery object set. Although similar to .each(), there is one big difference: .each() returns the same jQuery object, but .map() returns a new jQuery object with the values returned by each iteration.

To illustrate using .map(), check out the following snippet of code. This code iterates through all <li> elements and returns an array containing the HTML content values inside the <li></li> elements shown in Figure 5.2:

01 <html> 02   <head> 03   <title>Python Phrasebook</title> 04   <meta charset="utf-8" /> 05   <script type="text/javascript" ...

Get jQuery and JavaScript Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.