Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the text of multiple elements split by delimiter using jQuery?

I am using jQuery to get the text value of multiple td elements in a row:

var getText = $(this).closest("tr").text();

The value of getText ends up being one long concatenated string of the text of each element with no breaks. Is it possible to add a delimiter for each new td element that it gets the text value from?

like image 330
mariahm24 Avatar asked Sep 30 '16 16:09

mariahm24


1 Answers

I'd first suggest retrieving an Array of the cells' text, rather than a single String:

var cellTexts = $(this).closest("tr").find('td').map(function(){
                  return $(this).text();
                }).get();

If you wish to have a single string, with a custom delimiter, you can then call Array.prototype.join(), and specify the delimiter you wish to use, for example a comma:

var cellTexts = $(this).closest("tr").find('td').map(function(){
                  return $(this).text();
                }).get(),
    allText = cellTexts.join(',');

References:

  • JavaScript:
    • Array.prototype.join().
  • jQuery:
    • closest().
    • find().
    • get().
like image 175
David Thomas Avatar answered Oct 26 '22 10:10

David Thomas