1. This website uses cookies. By continuing to use this website you are giving consent to cookies being used.
    For information on cookies and how you can disable them visit our Cookie Usage page.
    Dismiss Notice

JavaScript HTML to Entities script

Discussion in 'Web Development' started by JavaScriptBank, Feb 13, 2010.

  1. This JavaScript converts special HTML characters to their entities version inside user entered data such as a TEXTAREA before the detail at javascriptbank.com/


    How to setup

    Step 1: Use JavaScript code below to setup the script
    JavaScript
    Code:
    <SCRIPT type=text/javascript>
    
    // HTML to Entities (form) script- By JavaScriptKit.com (http://www.javascriptkit.com)
    // For this and over 400+ free scripts, visit JavaScript Kit- http://www.javascriptkit.com/
    // This notice must stay intact for use
    
    function html2entities(){
    var re=/[(<>"'&]/g
    for (i=0; i<arguments.length; i++)
    arguments[i].value=arguments[i].value.replace(re, function(m){return replacechar(m)})
    }
    
    function replacechar(match){
    if (match=="<")
    return "&lt;"
    else if (match==">")
    return "&gt;"
    else if (match=="\"")
    return "&quot;"
    else if (match=="'")
    return "'"
    else if (match=="&")
    return "&amp;"
    }
    
    
    </SCRIPT>
        <!--
            This script downloaded from www.JavaScriptBank.com
            Come to view and download over 2000+ free javascript at www.JavaScriptBank.com
        -->
    
    Step 2: Copy & Paste HTML code below in your BODY section
    HTML
    Code:
    <FORM><TEXTAREA style="WIDTH: 400px; HEIGHT: 100px" name=data1></TEXTAREA><BR>
    <INPUT onclick=html2entities(this.form.data1) type=button value="Convert special chars to entities"> 
    </FORM>
        <!--
            This script downloaded from www.JavaScriptBank.com
            Come to view and download over 2000+ free javascript at www.JavaScriptBank.com
        -->
    

    Javascript floating message - Javascript multi level drop down menu - JavaScript in_array()​