HTML html-parsing JAVASCRIPT

[Solved – 3 Answers] How to remove html tags from string

[Solved - 3 Answers] JAVASCRIPT - remove html tag - How to remove html tags from string,Is there an easy way to remove html tag from string?


Is there an easy way to remove html tag from string?

If we’re running in a browser, then the easiest way is just to let the browser do it for us.

Javascript Code
function strip(html)
{
   var tmp = document.createElement("DIV");
   tmp.innerHTML = html;
   return tmp.textContent || tmp.innerText || "";
}

An improvement to an earlier answer.

Javascript Code
function strip(html)
{
   var tmp = document.implementation.createHTMLDocument("New").body;
   tmp.innerHTML = html;
   return tmp.textContent || tmp.innerText || "";
}
This way something running like this will do no harm:

strip("<img onerror='alert(\"could run arbitrary JS here\")' src=bogus>")

This is the Simplest way:

Javascript Code

jQuery(html).text();

That retrieves all the text from a string of html
READ  JAVASCRIPT - .prop() vs .attr()

About the author

Venkatesan Prabu

Venkatesan Prabu

Wikitechy Founder, Author, International Speaker, and Job Consultant. My role as the CEO of Wikitechy, I help businesses build their next generation digital platforms and help with their product innovation and growth strategy. I'm a frequent speaker at tech conferences and events.

Add Comment

Click here to post a comment