bigdave
bigdave

Reputation: 337

jQuery insert an element after a comment tag

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

Upvotes: 0

Views: 97

Answers (1)

Bram Vanroy
Bram Vanroy

Reputation: 28564

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>

Upvotes: 2

Related Questions