I recently came across an issue where I need to strip the html tags from the data before displaying it on screen.
The data came this way:
<p><span style="color: rgb(68, 68, 68);">Sample data which i want to get.</span></p>
What have I tried
In order to solve this issue, I created a div, added the html with innerHtml
and extract the text with textContent
.
function strip(html)
{
var tmp = document.createElement("DIV");
tmp.innerHTML = html;
return tmp.textContent || tmp.innerText || "";
}
The above code has been taken from stackoverflow.
The problem
I'm concerned if this is the correct thing to do, as this is creating extra divs every time we call the strip
function.
The question
Is there any better way to accomplish this?
Sample data which i want to get.
' })` How to get the text content from this? – Anurag Srivastava Mar 17 '19 at 15:36