• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Addon / Remove HTML Tags

Remove HTML Tags

This function will remove all HTML tags from a string.

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>.

Addon

Related Snippets:

  • Multiple onLoad 2
  • Window Print Method
  • getXML
  • getElementsByAttribute

Primary Sidebar

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2025 JavaScriptSource.com

  • About
  • Privacy Policy
  • FAQ
  • Jobs For Developers