Adrian Enriquez
Adrian Enriquez

Reputation: 8413

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?

Upvotes: 4

Views: 1691

Answers (1)

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67207

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

Upvotes: 8

Related Questions