How to remove HTML Tags from a string in Javascript
Some days back, one of my team members came to me saying he need a function which is capable of remove the html tags using javascript. I mean if we pass “ this is demo ” this string to the function. It supposes to return only “This is demo” and the main thing is with having no formatting at all, Just the simple text between tags.
So, Initially I make it happen using regular expression but the strategy fails when some think like comes in. after snatching my hairs, putting some hard hand on the keyboard just before the moment I go for suicide J the following code did the trick.
function removeHTMLTags(htmlString){
if(htmlString){
var mydiv = document.createElement(”div”);
mydiv.innerHTML = htmlString;if (document.all) // IE Stuff
{
return mydiv.innerText;}
else // Mozilla does not work with innerText
{
return mydiv.textContent;
}
}
}




















thanks usman,very valuable
wow what a wonderful way you described it really helps me alot thanks mr agha usman keep doing this sort work for helping more pplz
It helped me alot. Thank you very much, Agha Usman.
Wow! Such a cool way to remove tags! As far as I can see, even the code that’s enclosed in script tags doesn’t evaluate this way.
Amazing, thanks!
this is really cool script thanks really works well