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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:00:01+00:00 2026-05-26T20:00:01+00:00

Ok, I have the following string I have a string that contains the following:

  • 0

Ok, I have the following string

I have a string that contains the following:

<span>A</span>BC<span id="blabla">D<span>EF</span></span>GH

I want to be able to use pure JavaScript to strip out any span tag that does not have an id so that the output looks like:

ABC<span id="blabla">DEF</span>GH

I have the following code which works fine but does not handle the nested span in the middle (the one that holds EF). I just need to know how can I use recursion to accomplish my goal.

function removeSpans2(s) {
  var a = document.createElement('div');
  a.innerHTML = s;
  var node, next = a.firstChild;

  while (node = next) {
    next = next.nextSibling

    if (node.tagName && node.tagName.toLowerCase() == 'span' && !node.id) {
      a.replaceChild(document.createTextNode(node.textContent || node.innerText), node);
    }
  }
  return a.innerHTML;
}
  • 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-26T20:00:02+00:00Added an answer on May 26, 2026 at 8:00 pm

    No recursion needed, figured out a simpler algorithm. This one handles all sorts of nested elements, you can have A elements inside the spans, or spans in side As, or spans in spans in spans… whatever.

    function removeSpans3(s) {
      var a = document.createElement('div');
      a.innerHTML = s;
      var span, spans = a.getElementsByTagName('span');
      var frag, arr = [];
    
      // Stabilise spans collection in array
      for (var i=0, iLen=spans.length; i<iLen; i++) {
        arr[i] = spans[i];
      }
    
      // Process spans
      for (i=0; i<iLen; i++) {
        span = arr[i];
    
        // If no id, put content into a fragment
        if (!span.id) {
    
          // Some older IEs may not like createDocumentFragment
          frag = document.createDocumentFragment();
    
          while (span.firstChild) {
            frag.appendChild(span.firstChild);
          }
    
          // Replace span with its content in the fragment
          span.parentNode.replaceChild(frag, span);
        }
      }
      return a.innerHTML;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string that contains the following: <span>A</span>BC<span id=blabla>D</span>EF i want to be
I have the following string: 2010-04-08T12:46:43+00:00 I want to convert that to: 8th April
I have the following regex that isn't working. I want to match the string
I have a string that contains a date, in the following format: dd-mm-yyyy with
If I have a string that contains the following: This is 1 test 123-456-7890
I currently have a list that contains the following CountryCode (string) CountryStr (string) RegionStr
Say, I have a line that contains the following string: $tom said blah blah
The following is in C++. I have a string that contains the environment variables
I have a string variable that contains the following html data: <p> <em><strong>This is
I have a String that contains the following: ?workarea=London+&+Home+Counties+Ltd&sub=fs&&&FASh*5 which resembles a URI query

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.