1

My code splits the text and place a html code after a certain number of words. It's working, the problem is that the split function not only splits text, but also splits HTML. How can I configure it to split text only? Let's say I have an article and I need a banner to appear in the middle of the article, if I don't take care, I'll split some div or something like that.

Here's the code:

<script type="text/javascript">
 jQuery(function($) {


var wordList = $(".newsitem_text").html().split(' ');
var newHtml = '';

$.each(wordList, function(index, word){
  newHtml += ' ' + word;
  if (index == 10) {
    newHtml += 'Some HTML goes here'     
  }        
})
;

$(".newsitem_text").html(newHtml);

});

</script>
j08691
  • 204,283
  • 31
  • 260
  • 272
Matheus Lopes
  • 315
  • 3
  • 14

1 Answers1

1

You should use jQuery's .contents() method and split only text tags while copying everything else verbatim. Something like this:

jQuery(function($) {

  var newHtml = '';
  var count = 0;

  function check() {
    if (count >= 20) {
      newHtml += 'Some HTML goes here'     
      count = 0;
    }
  }

  $(".newsitem_text").contents().each(function () {

    if (this.nodeType != 3) {
      newHtml += this.outerHTML;
      count += $(this).text().trim().split(/\s+/).length;
    }
    else {
      var wordList = $(this).text().trim().split(/\s+/);

      $.each(wordList, function(index, word){
        count++;
        check();
        newHtml += ' ' + word;
      })
    }

  });

  check();

  $(".newsitem_text").html(newHtml);

});
a sad dude
  • 2,775
  • 17
  • 20
  • and remove the ` jQuery(function($) { var wordList = $(".newsitem_text").text().split(' '); ` ??? – Matheus Lopes Nov 05 '15 at 02:19
  • Wrap it all in `jQuery(function($) { })`, yes, but remove the rest. – a sad dude Nov 05 '15 at 02:23
  • Did that, all I got was some html wrapped `undefined .colunasocial1 { padding-left:4px;} .centralizasocial { margin-left: auto; margin-right:auto} Tweetar !function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs"); (function(d, s,Some HTML goes here id) { var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) return;` – Matheus Lopes Nov 05 '15 at 02:52
  • Isn't there a way to make it place the html after the html close tags? For example place after `>`, like placing after `` or `` – Matheus Lopes Nov 05 '15 at 02:56
  • Sorry, it was supposed to be nodeType *not equal* to three. Edited. Works for me now. Not sure I understand your last comment. – a sad dude Nov 05 '15 at 03:04
  • Ah, got it. You want to place something right afrer every non-text node? Just add whatever you want in the first if condition, e.g. `newHtml += this.outerHTML + 'and so on';` – a sad dude Nov 05 '15 at 03:06
  • This code is splitting the html source. If I configure it to appear after 40 words, it will not only count the words, but also the whole html, for example I have the code `This is Link `, this is counted as 5 words, so if I configure the code to appear after the 3 word ``, it will appear inside the link. Got it? – Matheus Lopes Nov 05 '15 at 03:10
  • Can you move this conversation to a chat? – Matheus Lopes Nov 05 '15 at 03:19
  • Edited it to do what you want. – a sad dude Nov 05 '15 at 03:20
  • It's because I have no idea on how to make the change that you suggested. – Matheus Lopes Nov 05 '15 at 03:31
  • http://stackoverflow.com/questions/4214099/jquery-split-at-html-tags-i-e-split-at-every-instance-of-the-h2-tags – Matheus Lopes Nov 05 '15 at 03:34
  • What are you having a problem with exactly? I already did all the changes in the answer, the code should just work. – a sad dude Nov 05 '15 at 03:48
  • Oh, I didn't see before. Just tested the code and it is working, but it is repeating the images, like a loop. – Matheus Lopes Nov 05 '15 at 04:05