I'm using this function to extract the Text from the Textareas in my project (to avoid saving the copied text styling in the database),
export const stripHTML = (text) => {
// eslint-disable-next-line no-var
var temporalDivElement = document.createElement("div");
temporalDivElement.innerHTML = text;
return temporalDivElement.textContent || temporalDivElement.innerText || "";
};
The problem is that now the user can't write any line breaks in the text. What is the best way to solve that so I get a clean text but with the line breaks?
You don't need the div, just do this:
var textarea = document.getElementById('textarea');
var temporalDivElement = document.createElement("div");
temporalDivElement.innerHTML = textarea.value;
var tempText = temporalDivElement.textContent || temporalDivElement.innerText || "";
return tempText.replace(/\r?\n/g, '<br />');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With