Working with the map method in jQuery
The map method calls a given function for each element of a collection. It is especially useful for getting or setting the values of a collection of elements.
The map method returns a newly formed set of different elements and objects, unlike the each method, which returns the original set of elements.
Let's say we have several paragraphs in our HTML code:
<p id="aaa">text1</p>
<p id="bbb">text2</p>
<p id="ccc">text3</p>
Let's use the map method to print a list of id element names separated by commas. For each p we'll get the id using map and get. Then we'll use the join JavaScript method to print the resulting values as a string:
alert(
$('p').map(
function() {
return this.id;
}
).get().join()
);
Using the map method, change the p values of all id to their ordinal numbers.