How to create a comma separated list of identifiers in JavaScript?

So, I have a <ul> containing <li> elements, and I would like to get the identifiers of these elements and queue them to another page.

Same:

 <ul id="myList"> <li id="first">First</li> <li id="second">Second</li> <li id="third">Third</li> </ul> 

in

 first,second,third 

Is there a neat way to do this? I have jQuery, so my brute force, probably not very, very, very good, is to use iteration with each() and build it that way. Sounds a little sloppy.

+4
source share
1 answer

Short and neat way to use .map :

 var ids = $("#myList li").map(function() { return this.id; }).get().join(","); 
+9
source

Source: https://habr.com/ru/post/1384914/


All Articles