Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to separate $('ul li').text by comma

HTML

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

jQuery

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

Current Output

List1List2

Expected Output

List1,List2

Question

How to separate them by comma?

like image 507
Adrian Enriquez Avatar asked Feb 13 '23 17:02

Adrian Enriquez


1 Answers

Try to use .map() along with .get() to get that texts into an array and .join() it afterwards,

var values = $('#tags li').map(function(){ 
  return $(this).text(); 
}).get().join(','); // List1,List2
like image 77
Rajaprabhu Aravindasamy Avatar answered Feb 15 '23 07:02

Rajaprabhu Aravindasamy