Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is easy enough, provided that you enclose the text after your heading tags with a container:</p> <pre><code>$(document).ready(function() { $('h1, h2, h3, h4, h5, h6').each( function(i, item) { if ( $(item).nextUntil('h1, h2, h3, h4, h5, h6').text() ) { $(item) .nextUntil('h1, h2, h3, h4, h5, h6').last() .after('&lt;a href="#top"&gt;Back To Top&lt;/a&gt;'); } }); }); </code></pre> <p>This works regardless of how many nodes you have after your headings, provided that the text is wrapped in a tag of some sort.</p> <p><strong>Update</strong> If you can't wrap text nodes with another tag, my suggestion is to use jQuery to do this first - this will allow us to process the code consistently, and work around the limitation of <code>nextUntil</code>. Update my code above to the following:</p> <pre><code>$(document).ready(function() { // wrap text nodes with a span $('#container') // note that I'm using a container reference to scope this - adjust as needed .contents() .filter(function() { return this.nodeType == 3; //Node.TEXT_NODE }).wrap('&lt;span class="temp" /&gt;'); $('h1, h2, h3, h4, h5, h6').each( function(i, item) { var items = $(item).nextUntil('h1, h2, h3, h4, h5, h6'); if ( items.text().match(/[^\s]/gi) ) { // Only insert a back to top link if these nodes contain at least one non-whitespace character items.last() .after('&lt;a href="#top"&gt;Back To Top&lt;/a&gt;'); } }); $('#container').find('span.temp').replaceWith(function () { return $(this).text(); }); }); </code></pre> <p>Here's a <a href="http://jsfiddle.net/THvM2/1/" rel="nofollow">jsfiddle</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload