Recently I required to remove HTML tags from html page. For that I have used following function which removes all HTML tags.
Source Code
Paste Your Source code into designated area.For Example.
<p><strong>Input code:</strong>
(<em>Note: Processing is done on the "input-code" id</em>)</p>
<pre>
<code id="input-code">
<h2>About</h2>
<p>Here you will find post about how to remove HTML tag from a string using
javascript.<p>
<p><a href="/about">Read more</a></p>
External File
Create RemoveHTML.js external file and paste following code.
function removeHTMLTagFromString()
{
if (document.getElementById && document.getElementById("input-code"))
{
var strHtmlCode = document.getElementById("input-code").innerHTML;
/* It replaces escaped brackets with real ones,
i.e. < is replaced with < and > is replaced with > */
strHtmlCode = strHtmlCode.replace(/&(lt|gt);/g,
function(strMatch, p1)
{
return (p1 == "lt") ? "<" : ">";
});
var strTagStrippedText = strHtmlCode.replace(/<\/?[^>]+(>|$)/g, "");
alert("Output text:\n" + strTagStrippedText);
}
}
{
if (document.getElementById && document.getElementById("input-code"))
{
var strHtmlCode = document.getElementById("input-code").innerHTML;
/* It replaces escaped brackets with real ones,
i.e. < is replaced with < and > is replaced with > */
strHtmlCode = strHtmlCode.replace(/&(lt|gt);/g,
function(strMatch, p1)
{
return (p1 == "lt") ? "<" : ">";
});
var strTagStrippedText = strHtmlCode.replace(/<\/?[^>]+(>|$)/g, "");
alert("Output text:\n" + strTagStrippedText);
}
}
Head
import javascript file into head section of HTML document.
import javascript file into head section of HTML document.
<script type="text/javascript" src="removeHTML.js"></script>
Body
Paste the code into BODY section, which HTML Tag you want to remove in HTML document.
<p><strong>Input code: </strong>( <em>Note: Processing is done on the "input-code" id </em>)</p>
<pre>
<code id="input-code">
<h2>About</h2>
<p>Here you will find post about how to remove HTML tag from a string using
javascript.<p>
<p><a href="/about">Read more </a></p>
</code>
</pre>
<a href="#" onclick="removeHTMLTagFromString(); return false;">
»Click to remove all HTML tags from the input code </a>.
<pre>
<code id="input-code">
<h2>About</h2>
<p>Here you will find post about how to remove HTML tag from a string using
javascript.<p>
<p><a href="/about">Read more </a></p>
</code>
</pre>
<a href="#" onclick="removeHTMLTagFromString(); return false;">
»Click to remove all HTML tags from the input code </a>.