-1

I want to add char at beginning of each line in html element like :

<div id="container">
  <p>first part here !</p>
  but they is text here too
  <span>and bla bla bla</span>
  foo
  <p>bar</p>
  baz
</div>

expected

<div id="container">
  <p>#first part here !</p>
  #but they is text here too
  <span>#and bla bla bla</span>
  #foo
  <p>#bar</p>
  #baz
</div>

render : https://jsfiddle.net/wna21a1q/

how can I do that whith JS (or jQuery)?

Like :

$('#container').addAtEachLine('#')
Jaydeep
  • 1,686
  • 1
  • 16
  • 29
Matrix
  • 3,458
  • 6
  • 40
  • 76

3 Answers3

4

You can loop over contents:

$("div").contents().each( function () {
  if(this.nodeType==1) {  //is an element
    $(this).prepend(document.createTextNode("#"))
  } else if ($.trim(this.nodeValue).length){ //text node that is more than a return
    this.nodeValue = "#" + this.nodeValue;
  }
})
span { display: block }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <p>first part here !</p>
  but they is text here too
  <span>and bla bla bla</span>
  foo
  <p>bar</p>
  baz
</div>
epascarello
  • 204,599
  • 20
  • 195
  • 236
  • good but I have another problem, I don't see it before : its when the width of `#conatiner` is at 100px(for exemple) and if there is a text(s) too long for take only one line. In this case the symbol `#` is not added for "each line", you understand? – Matrix Oct 02 '17 at 15:52
  • 1
    Well that is really a different question than what I answered.... That is basically impossible to detect with code like this since word wrap in display does not have line breaks.... – epascarello Oct 02 '17 at 15:54
  • Probably would have to do something like https://stackoverflow.com/questions/3738490/finding-line-wraps – epascarello Oct 02 '17 at 15:55
0

@epascarello has provided a super answer to this question already. I was playing around with it and found a way of condensing the code a bit further. Probably not everybody's favourite but it reduces the actual prepend-action to a single line:

$("div").contents().each((i,obj)=>{
  var p=['','innerHTML','','nodeValue'][obj.nodeType]; // pick method for text access
  var t=obj[p].trim();       // get text and remove surrounding blanks
  if(t.length) obj[p]='#'+t; // if text>"" prepend a "#"
})
span { display: block }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <p>first part here !</p>
  but they is text here too
  <span>and bla bla bla</span>
  foo
  <p>bar</p>
  baz
</div>
Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43
0

and here my vanilla.js version.

var el = document.querySelector('div');

function addchar(item) {

  var childNodes = item.childNodes;
  var countNode = childNodes.length;

  for (var i = 0; i < countNode; i++) {
    if (childNodes[i].nodeType !== 3) {
      addchar(childNodes[i]);
    } else {
      if (childNodes[i].nodeValue.trim()) {
        childNodes[i].nodeValue = "# " + childNodes[i].nodeValue;
      }
    }
  }
}

addchar(el);
span {display:block}
    <div>
      <p>first part here !</p>
      but they is text here too
      <span>and bla bla bla</span>
      foo
      <p>bar</p>
      baz
    </div>
quaerere
  • 1
  • 1