How to separate $ ('ul li') text with a comma

HTML

<ul>
  <li>List 1</li>
  <li>List 2</li>
<ul>

JQuery

$('ul li').text();

Current output

List1List2

Expected Result

List1,List2

Question

How to separate them with a comma?

+4
source share
1 answer

Try using .map()along with .get()to get these texts into an array and .join()after that,

var values = $('#tags li').map(function(){ 
  return $(this).text(); 
}).get().join(','); // List1,List2
+7
source

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


All Articles