CODEDIGEST
Home » CodeDigest
Search
 

Technologies
 

How to strip HTML tags, HTML Encode and Decode HTML string using jQuery/JavaScript in ASP.Net?
Submitted By Satheesh Babu B
On 3/28/2011 7:51:45 AM
Tags: ASP.NET,CodeDigest,jquery  

How to Strip/Remove HTML tags, HTML Encode and Decode HTML string using jQuery/JavaScript in ASP.Net?

The jQuery library has 2 useful methods called $.html() and $.text() which can be used in this scenarios.


The below code snippet will help us to strip or remove the html tags from a string, HTML encode and decode a string in client side scripting.

<script src="_scripts/jquery-1.4.1.min.js" type="text/javascript"></script>

    <script type="text/javascript">

        $(function() {      

        $("#dvHTMLStripped").html($("#dvHTML").text());

        $("#dvHTMLEncoded").text($("#dvHTML").html());

        $("#dvHTMLDecoded").html($("#dvHTMLEncoded").text());

        });

   

    </script>

</head>

<body>

    <form id="form1" runat="server">

    <div>

    <b>Source:</b>

    <div id="dvHTML"> <small>This</small> is <b>jQuery Sample.</b></div>

    <br />

   

    <b>HTML Stripped:</b> <div id="dvHTMLStripped"></div>

    <b>HTML Encoded:</b> <div id="dvHTMLEncoded"></div>

    <b>HTML Decoded:</b> <div id="dvHTMLDecoded"></div>

    </div>

    </form>

In the above code, the div tag dvHTMLStripped will display the text after stripping the html tags. Similarly, the dvHTMLEncoded and dvHTMLDecoded div tag will display the encoded and decoded version of the html string found in dvHTML div tag.

Happy Coding!!

Do you have a working code that can be used by anyone? Submit it here. It may help someone in the community!!

Recent Codes
  • View All Codes..