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

  • Home
  • SEARCH
  • 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 5936091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:20:58+00:00 2026-05-22T15:20:58+00:00

I need to use javascript to remove all html tags, except for those I

  • 0

I need to use javascript to remove all html tags, except for those I explicitly allow. I have a form that only allows the following tags and their respective end tags:

<b> <strong> <i> <em> <u> <br> <pre>
<blockquote> <ol> <ul> <li> 
<a href="http://www.somesite.com">link</a>

All other markup should be removed. I have been searching but I have only found instances where all tags are removed or a single tag is removed. Can this be done simply? I cannot use PHP, must be javascript. Any solutions?

Thanks!

  • 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-05-22T15:20:59+00:00Added an answer on May 22, 2026 at 3:20 pm
    jQuery.fn.removeTags = function()
    {
        this.each(function()
        {
            if(jQuery(this).children().length == 0)
            {
                jQuery(this).replaceWith(jQuery(this).text());
            }
            else
            {
                jQuery(this).children().unwrap();
            }
        });
        return this;
    };
    
    jQuery("#container").find(":not(b, strong, i, em, u, br, pre, blockquote, ul, ol, li, a)").removeTags();
    

    Make sure the container is nothing higher than the body tag. Or you might have issues when it takes out head, html, script etc. tags.

    Also if you want the :not could be a list and you could:

    var mylist = ["b" ,"strong", ... etc. etc.];
    jQuery(":not(" + mylist.join(", ") + ")").removeTags();
    

    Or even put this in the removeTags function. (the possibilities are endless … )

    EDIT: as some have noted in the comments: Javascript can be turned off. The other thing is I assumed you wanted to keep all the inner information. If not then just a remove() will suffice as megakorre suggests.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have html page and I need remove all div except the div with
I have a site that I've developed that makes decent use of Javascript, and
I can't use JQuery sadly I need to use good old Javascript. I have
I need to use sed to convert all occurences of ##XXX## to ${XXX} .
I have a YUI dialog that submits a form to a Java servlet. The
I have a remote form with hand-made buttons using tags. I'm using data-remote=true in
I am trying to load a html page through UIWebview.I need to disable all
I have a project where I need to create a desktop app that acts
I want to totally remove the standard ASP.NET membership system and use Facebook only
I have a form that is a validation field, there is a rand() server

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.