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 8231647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:34:04+00:00 2026-06-07T17:34:04+00:00

I have a variable that contains a string of text and html tags, such

  • 0

I have a variable that contains a string of text and html tags, such as:

var temp = "<div>Some text</div><p>More text<span>here</span></p><p>Even more</p>";

I would like to remove all tags of a certain type. Let’s say all p and span tags for example.

This is the best I can come up with:

var temp = "<div>Some text</div><p>More text<span>here</span></p><p>Even more</p>";
var $temp = $(temp);
$("p", $temp).replaceWith("foo");
alert($temp.html());  //returns "Some text"

The closest response I could find is this answer by Nick Craver: strip span tags from string with jquery.

  • 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-07T17:34:05+00:00Added an answer on June 7, 2026 at 5:34 pm

    Demo: http://jsfiddle.net/VwTHF/1/

    $('span, p').contents().unwrap();
    

    .contents() will get the elements and text within each such tag, and .unwrap will remove the element wrapping each content section.

    Based on your current approach it would look something like this:

    var temp = "<div>Some text</div><p>More text<span>here</span></p><p>Even more</p>";
    var $temp = $(temp);
    $temp.find('span, p').contents().unwrap().end().end();
    

    If you want to continue targeting the original object, you have to use .end() to clear the filter.

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

Sidebar

Related Questions

I have a variable that contains some text, some html, basically can be a
I have a string variable that contains the following html data: <p> <em><strong>This is
I have a div that contains some html and a lot of Javascript <div
Think you have a variable that contains a string of text with spaces inbetween
I have a variable that contains the following JSON string: { 0 : Jun
I have a query string that contains a variable like this $field_name = 'features';
I have a variable that contains the entire html for a page as a
If I have a variable that contains text information (say taken from a textarea),
I have an string input-buffer that contains html. That html contains a lot of
A need some help, I have a TextBlock that contains a string like this

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.