1

Here is a simple table

<table id="tbl">
<tr>
<th id='first'>First<br>Name</th>
<th id='last>Last name</th>
</tr>

if I run a simple console.log(document.getElementById("first").innerText) I got results of "First\nName" which is perfect as I see the full text and the \n for the
in the text.

Now if I try to use cloneNode() to get data from the table, the \n is stripped out of the innerText

tb = document.getElementById("tbl").cloneNode(true);
mydata = tb.rows;
let el = mydata[0].children;
for (let j = 0; j < el.length; j++) {
    my_data = el[j].innerText;
    console.log(my_data);
}

This will result with "FirstName" "Last Name" I really need to see the \n in the innerText but it is being filtered out. How can I do this and get the \n in the innnerText?

  • 1
    [It seems like the newline only shows up after the element is rendered](https://jsbin.com/remesuj/1/edit?html,js,console,output). Although the `
    ` tag exists at all times. As a guess, `.innerText` is not very consistent. Usually `.textContent` is more stable. However, that doesn't show a new line at all.
    – VLAZ May 18 '21 at 15:43
  • Possible duplicate of https://stackoverflow.com/questions/55804438/interesting-conversion-of-br-between-innerhtml-and-innertext – Tom Jun 01 '21 at 06:36

0 Answers0