Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8410031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:04:32+00:00 2026-06-10T00:04:32+00:00

I have a <textarea> that a user types something in, and they are allowed

  • 0

I have a <textarea> that a user types something in, and they are allowed to type html. Once they are done typing, the <textarea> changes back to a <span> that contains what they just typed. However, I want to strip out certain tags such as <script>, <div>, etc… before I put it back into the <span>.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T00:04:34+00:00Added an answer on June 10, 2026 at 12:04 am

    Believe it or not you can (safely) do this with the browser’s built in HTML parser. Simply create a new div with document.createElement, toss the contents of the textarea into the div using innerHTML, and presto, you’ve got a full blown DOM to work with. And no, scripts contained within this div will not be evaluated.

    Here’s a simple example that strips from an element all tags that do not appear in an ALLOWED_TAGS list.

    var ALLOWED_TAGS = ["STRONG", "EM", "BLOCKQUOTE", "Q", "DEL", "INS", "A"];
    
    function sanitize(el) {
        "Remove all tags from element `el' that aren't in the ALLOWED_TAGS list."
        var tags = Array.prototype.slice.apply(el.getElementsByTagName("*"), [0]);
        for (var i = 0; i < tags.length; i++) {
            if (ALLOWED_TAGS.indexOf(tags[i].nodeName) == -1) {
                usurp(tags[i]);
            }
        }
    }
    
    function usurp(p) {
        "Replace parent `p' with its children.";
        var last = p;
        for (var i = p.childNodes.length - 1; i >= 0; i--) {
            var e = p.removeChild(p.childNodes[i]);
            p.parentNode.insertBefore(e, last);
            last = e;
        }
        p.parentNode.removeChild(p);
    }​
    

    As mentioned, you’ll have to create an empty div container to use this. Here’s one example application of the technique, a function to sanitize strings. Please note, however, that “sanitize” is at this time a misnomer–it will take a lot more work (cleaning attribute strings and such) before this “sanitizer” will output HTML that is truly safe.

    function sanitizeString(string) {
        var div = document.createElement("div");
        div.innerHTML = string;
        sanitize(div);
        return div.innerHTML;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an HTML textarea that is of fixed width, but variable height. I
The user types some text. When they press a button, what they have typed
So I have an input box, and when a user types something in it
I have a webpage that the user inputs data into a textarea and then
I currently have a page that allows a user to enter different types of
I've been looking for something that will authorize a textbox when user types something
I would like to have something like <textarea>Welcome, click here to type.</textarea> Then clear
I have a textarea that I might want to disable in certain conditions. I
I have a textarea that I'm able to edit with the jeditable plugin but
I have a textarea that is used as the body of an email sent

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.