0

In Javascript, I have access to a HTML-DOM-node with the following HTML-code (.innerHTML):

Some Text <b class="first">some other text</b> <a href="link">even more  text</a> text <b><span class="second">text text</span></b> and <span class="third">more text</span>text<br>
Some Text <b class="first">some other text</b> <a href="link">even more text</a> text <b><span class="second">text text</span></b> and <span class="third">more text</span>text<br>
Some Text <b class="first">some other text</b> <a href="link">even more text</a> text <b><span class="second">text text</span></b> and <span class="third">more text</span>text<br>
Some Text <b class="first">some other text</b> <a href="link">even more text</a> text <b><span class="second">text text</span></b> and <span class="third">more text</span>text<br>
Some Text <b class="first">some other text</b> <a href="link">even more text</a> text <b><span class="second">text text</span></b> and <span class="third">more text</span>text<br>
Some Text <b class="first">some other text</b> <a href="link">even more text</a> text <b><span class="second">text text</span></b> and <span class="third">more text</span>text<br>
<script>
    ...
</script>
    <script type="text/javascript">
   ...
    </script>

<script type="text/javascript">
 ...

</script>

Important: each text-line ends with a break <br>.

I want to enclose each textline in a <span class="myclass"></span> such that I can access each line, i.e., subdivide the big node into several smaller nodes with respect to the breaks. Is there a method in Javascript or jquery to do this? Output should be:

<span class="myclass">Some Text <b class="first">some other text</b> <a href="link">even more text</a> text <b><span class="second">text text</span></b> and <span class="third">more text</span>text<br></span>
<span class="myclass">Some Text <b class="first">some other text</b> <a href="link">even more text</a> text <b><span class="second">text text</span></b> and <span class="third">more text</span>text<br></span>
<span class="myclass">Some Text <b class="first">some other text</b> <a href="link">even more text</a> text <b><span class="second">text text</span></b> and <span class="third">more text</span>text<br></span>
<span class="myclass">Some Text <b class="first">some other text</b> <a href="link">even more text</a> text <b><span class="second">text text</span></b> and <span class="third">more text</span>text<br></span>
<span class="myclass">Some Text <b class="first">some other text</b> <a href="link">even more text</a> text <b><span class="second">text text</span></b> and <span class="third">more text</span>text<br></span>
<span class="myclass">Some Text <b class="first">some other text</b> <a href="link">even more text</a> text <b><span class="second">text text</span></b> and <span class="third">more text</span>text<br></span>
<script>
    ...
</script>
    <script type="text/javascript">
   ...
    </script>

<script type="text/javascript">
 ...

</script>

1 Answer 1

1
// I assume you can have your content here
var div = document.getElementById('divWithContent');
// filter out script nodes
var tmp = document.createElement('div');
for (var i=0;i<div.childNodes.length;i++) {
  var item = div.childNodes.item(i);
  if (!(item.nodeType === 1 && item.nodeName === 'SCRIPT')) {
    var tmp2 = item.cloneNode();
    tmp2.innerHTML = item.innerHTML;
    tmp.appendChild(tmp2);
  }
}
// split by <br> and wrap in <span>
var lines = tmp.innerHTML.split('<br>').filter(function(x){
  return x && x.trim() != ''
});
tmp.innerHTML = lines.map(function(x){
  return '<span class="myWrap">' + x + '<br></span>'
}).join('\n');
console.log(tmp.innerHTML);
// if you're happy with result you can place it where you want
div.innerHTML = tmp.innerHTML;
Sign up to request clarification or add additional context in comments.

3 Comments

Oh yes thank you very much, I got closer to the desired output, but your method eliminates some text. I get: <span class="myWrap">Some Text <b class="first"></b> <a href="link"></a> text <b></b> and <span class="third"></span>text<br></span> instead of <span class="myWrap">Some Text <b class="first">some other text</b> <a href="link">even more text</a> text <b><span class="second">text text</span></b> and <span class="third">more text</span>text<br></span>
I've found the error. item.cloneNode() does not clone the innerHTML of some nodes. It works with: var tmp2 = item.cloneNode(); tmp2.innerHTML = item.innerHTML; tmp.appendChild(tmp2);
good! not sure about between browsers portability but should work otherwise

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.