I'm trying to retrieve some text from a div with the contentEditable attribute and it's working perfectly fine.
I noticed however that deep cloning the element containing the text would make .innerText
completely unusable, as text layout, such as carriage returns or line breaks are apparently gone from the property.
Here is a working example → https://jsfiddle.net/TomWTG/otuakz6q/8/
Here's the related code :
<div contenteditable="true" class="input_text">
This is a placeholder.
<div>
<br />
</div>
<div>
The issue I'm having is that the linebreaks and the carriage returns are
completely gone from the deep clone's .innerText property.
</div>
<div>
<br />
</div>
<div>Try and convert it to see the text layout removed.</div>
</div>
<button>convert</button>
const btn = document.querySelector('button')
const inputText = document.querySelector('.input_text')
btn.addEventListener('click', e => {
const tempText = inputText.cloneNode(true)
console.log(tempText.innerText)
inputText.innerHTML = tempText.innerText
})
As an example, let's say that :
inputText.value =
"Hello World, foo bar.
foo bar."
the expected output coming from tempText.innerText
would be :
"Hello World, foo bar.
foo bar."
but turns out to be :
"Hello World, foo bar.foo bar."
so my question is : is there a way I could clone the text and still have access to the both the HTML children and the InnerText at the same time ?
Thanks in advance !
` – Andreas May 26 '21 at 12:04