0

I can remove all html tags from the text but I cannot remove just the structure in span tags with data-word inside ...

function strip(html)
{
   var tmp = document.createElement("DIV");
   tmp.innerHTML = html;
   return tmp.textContent || tmp.innerText || "";
}

Original is:

I <span data-word="word1" class="synonyms" title="word2">word3</span> <b>word4<b>.

The result should be:

I word3 <b>word4</b>. 

With the script from above the result I get is:I word3 word4. So the remaining html is not preserved.

It is code from Strip HTML from Text JavaScript.

3
  • 1
    That should be </b> not <b>. Commented Apr 23, 2016 at 15:31
  • 1
    So you want to only remove span tags that have a data-word attribute (and replace them by their content)? Commented Apr 23, 2016 at 15:32
  • Yes just this and nothing else. Commented Apr 23, 2016 at 15:32

1 Answer 1

1

Select the elements you want to remove, replace them by their inner HTML and take the inner HTML.

function stripDataWordTags(container) {
  var node = container.cloneNode(true);
  Array.prototype.slice.call(node.getElementsByClassName("synonyms"))
    .forEach(function(a, i) {
      a.parentElement.insertBefore(document.createTextNode(a.innerHTML), a);
      a.parentElement.removeChild(a);
    });
  return node.innerHTML;
}

// Demo and usage:
alert(stripDataWordTags(document.getElementById("test")));
<div id="test">
  I <span data-word="test" class="synonyms">love</span> <b>ECMAScript 5</b>.
</div>

Sign up to request clarification or add additional context in comments.

7 Comments

Hello, the word1 is not the same and the class is (class="synonyms"). So this doesn't work in majority of cases.
@Brana Not sure what you mean… it doesn’t depend on the content of the attribute. If you want to select it by class synonyms, then say so in your question. You can easily change the selector to whatever you want, although for classes you can use getElementsByTagName instead of querySelectorAll, as the browser support for the prior is better.
The oputput with this code is the same as input - I <span data-word="test" class="synonyms">love</span> <b>ECMAScript 5</b> ... please check it.
@Brana Oh, getElementsByTagName should be getElementsByClassName. It works now, anyway.
@Brana Alright, now it really works! I thought, it would already work with multiple elements, but I know why it failed: I used getElementsByClassName, which returns an HTMLCollection, which is a live element collection. The first forEach call removes the first occurrence, the second call can’t be executed, because there is no second element anymore. slice-ing the HTMLCollection first, works. querySelectorAll however returns a NodeList which doesn’t have that problem, so it even works without the slice.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.