To preserve line breaks when getting text from a textarea with JavaScript, we can replace whitespace characters with '<br>\n'
.
For instance, we write
const post = document.createElement("p");
post.textContent = postText;
post.innerHTML = post.innerHTML.replace(/\n/g, "<br>\n");
to call replace
to replace all the \n
characters with '<br>\n'
.
One reply on “How to preserve line breaks when getting text from a textarea with JavaScript?”
thank you it was awsome!