0

I am trying to convert a HTML TABLE element into CSV format. In scanning the table, I am iterating visible TR elements, then within each one, I am iterating visible TD elements, using the helpful rule from this question: $(element).("*:not(:has(*)):visible").text() to retrieve only text from visible elements.

However, if the TD contains multiple elements, each with its own text, there are no spaces or line breaks between the text from these elements. Therefore, something that originally appeared in two lines, such as

<div>First line</div>
<div>Second line</div>

would be returned by text() as First lineSecond line.

Even if I add line breaks in the original between the DIVs, these are not returned.

How can I return text, but add a line break or any desired character between the text of each element in the selection set? Must I iterate these with the each() function instead, appending line break and next element's text to some result, such as:

let result = "";
$(element).("*:not(:has(*)):visible").each(function() {
   result += $(this).text() + "\n";
}
Ryan Griggs
  • 2,457
  • 2
  • 35
  • 58

0 Answers0