Total Pageviews

6/10/2013

Remove HTML Tags from a string




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);
            }
         }

Head

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


9 comments:

maheswari said...

Really a great post. Thanks for sharing this unique information with us. I get admired by reading your post. Keep on Blogging
Datawarehousing Training in Chennai

essay writing said...

Remowing HTML tags from the string is not a difficult thing to do.

sivanesan said...

The best thing is that your blog really informative thanks for your great information!
erp in chennai

Simmi Sharma said...

Cinematographer in jaipur
Great post and very helpful for me.Thanks for share this post

Sasha said...

Escorts service in jaipur

good knowledge for your post thanks

sapanchoudhary said...

The article shares very meaningful information, at least for me. Thanks for sharing, my friend! Please visit my website hope you enjoy it

jaipur call girls service
jaipur call girls service
chittorgarh call girls service

vip call girls in jaipur
vip call girls in jaipur
vip call girls in chittorgarh

call girls service jaipur
call girls service jaipur
call girls service chittorgarh

leading writers helpers uk said...

The using a function of this kind will allow you to remove the tags without any additional operations. However, we should upload the appropriate files for this.

preethi Shetty said...

This is the best place where we can get good collection of information with very useful stuff...
Best Online Software Training Institute | HTML5 CSS3 Training

Vicky Ram said...

It is a great post. Keep sharing such kind of useful information.

http://easyblogging
Article submission sites

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Affiliate Network Reviews