0

I'm trying to insert <br/><br/> after the comment tag <!-- pagebreak --> but I'm not sure how to achieve this? my code so far:

html:

<div class="tab-content">
  some content
  <!-- pagebreak -->
  some more content
  <!-- pagebreak -->
  some more content
</div>

jQuery:

$( ".tab-content" )
  .contents()
  .filter(function() {
  return this.nodeType === 8;}
  .each().after( "<br/><br/>" ).end());

Can anyone advise on how can achieve this?

Thanks

bigdave
  • 337
  • 3
  • 15
  • 1
    have a look at this. https://stackoverflow.com/questions/1623734/selecting-html-comments-with-jquery?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – chaos505 May 16 '18 at 14:21

1 Answers1

2

You were close. You don't need the each.

$(".tab-content").contents().filter(function() {
  return this.nodeType == 8;
}).after("<br><br>")
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="tab-content">
  some content
  <!-- pagebreak -->
  some more content
  <!-- pagebreak -->
  some more content
</div>

Resulting HTML:

<div class="tab-content">
  some content
  <!-- pagebreak --><br><br>
  some more content
  <!-- pagebreak --><br><br>
  some more content
</div>
Bram Vanroy
  • 27,032
  • 24
  • 137
  • 239