Note that there are some explanatory texts on larger screens.

plurals
  1. POAppend element only when "content/text" is found
    primarykey
    data
    text
    <p>Is it possible, with jquery, to do the following: </p> <p>Given a document marked up with h1 through h6, append a "back to top" link after every heading element only if there is text written after the heading and before the next.. </p> <p>So:</p> <pre><code>&lt;h1&gt;h1&lt;/h1&gt; &lt;p&gt;text&lt;/p&gt; &lt;h2&gt;h2&lt;/h2&gt; &lt;h3&gt;h3&lt;/h3&gt; text text &lt;h4&gt;h4&lt;/h4&gt; &lt;br&gt;&lt;br&gt; &lt;h5&gt;h5&lt;/h5&gt; &lt;br&gt;&lt;br&gt;text &lt;h6&gt;h6&lt;/h6&gt; &lt;div&gt;test&lt;/div&gt; </code></pre> <p>Would result in </p> <pre><code>&lt;h1&gt;h1&lt;/h1&gt; &lt;p&gt;text&lt;/p&gt; &lt;a..&gt;back to top&lt;/a&gt; &lt;h2&gt;h2&lt;/h2&gt; &lt;h3&gt;h3&lt;/h3&gt; text text &lt;a..&gt;back to top&lt;/a&gt; &lt;h4&gt;h4&lt;/h4&gt; &lt;br&gt;&lt;br&gt; &lt;h5&gt;h5&lt;/h5&gt; &lt;br&gt;&lt;br&gt;text &lt;a..&gt;back to top&lt;/a&gt; &lt;h6&gt;h6&lt;/h6&gt; &lt;div&gt;test&lt;/div&gt; &lt;a..&gt;back to top&lt;/a&gt; </code></pre> <p>So text can be in paragraphs, divs, spans, or not marked up at all... </p> <p>I'm not sure how to do this, and how to even select the last element before the next (or what if it's the last ?) to append the link to. </p> <p>Any help (doesn't have to be a complete solution) is welcome.</p> <p><strong>Edit: The biggest issue is this:</strong></p> <p>Unfortunately I'm working with a cms that doesn't output paragraphs, but instead outputs </p> <pre><code> &lt;h1&gt;..&lt;/h1&gt;&lt;br&gt;text&lt;br&gt;&lt;br&gt;&lt;h2&gt;h2&lt;/h2&gt; </code></pre> <p>and so on..</p> <p>So also: </p> <pre><code>&lt;h1&gt;test&lt;/h1&gt; &lt;br&gt; &lt;h2&gt;test&lt;/h2&gt; </code></pre> <p>using the second solution presented here, that adds a top link when it shouldn't.. I'm not sure how to prevent that. </p> <p><strong>Edit 2:</strong></p> <p>Tried changing to: </p> <pre><code>$('#content').find('h1, h2, h3, h4, h5, h6').each( function(i, item) { var nextItems = $(item).nextUntil('h1, h2, h3, h4, h5, h6'); if (nextItems.filter('p, div, span').size() &gt; 0 || nextItems.filter(function() { return this.nodeType == Node.TEXT_NODE; }).size() &gt; 0) { nextItems.last().after('&lt;p&gt;&lt;a href="#toc-top" class="toc-top"&gt;Back To Top&lt;/a&gt;&lt;/p&gt;'); } }); </code></pre> <p>but it's never finding text nodes.. Any reason why? contents is just the h2 followed by br and text...</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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