General
This function will remove all HTML tags from a string.
Notes
- Created by: Robert Nyman
- Web Site: http://robertnyman.com/
- Posted: February 5, 2007
Source Code
Paste this source code into the designated areas.
External file
Paste this code into an external JavaScript file named: removeHTMLTags.js
/* This script and many more are available free online at
The JavaScript Source!! http://javascriptsource.com
Created by: Robert Nyman | http://robertnyman.com/ */
function removeHTMLTags(){
if(document.getElementById && document.getElementById(“input-code”)){
var strInputCode = document.getElementById(“input-code”).innerHTML;
/*
This line is optional, it replaces escaped brackets with real ones,
i.e. < is replaced with < and > is replaced with >
*/
strInputCode = strInputCode.replace(/&(lt|gt);/g, function (strMatch, p1){
return (p1 == “lt”)? “<” : “>”;
});
var strTagStrippedText = strInputCode.replace(/</?[^>]+(>|$)/g, “”);
alert(“Output text:n” + strTagStrippedText);
// Use the alert below if you want to show the input and the output text
// alert(“Input code:n” + strInputCode + “nnOutput text:n” + strTagStrippedText);
}
}
Head
Paste this code into the HEAD
section of your HTML document.
<script type=”text/javascript” src=”removeHTMLTags.js”></script>
Body
Paste this code into the BODY
section of your 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 posts about news, trends and developing for internet,
mainly focusing on browsers and web user interfaces.</p>
<p><a href=”/about”>Read more</a></p>
</code>
</pre>
<a href=”#” onclick=”removeHTMLTags(); return false;”>» Click to remove all HTML tags from the input code</a>.