Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to nest anchor links via jQuery?
    text
    copied!<p>Looking for a great way to add anchor links within my HTML document after my pattern of H3 and the last p element block.</p> <p>This is my original HTML</p> <pre><code>&lt;div id="container"&gt; &lt;h3 id="faqa"&gt;title&lt;/h3&gt; &lt;p&gt;content&lt;/p&gt; &lt;h3 id="faqb"&gt;title&lt;/h3&gt; &lt;p&gt;content&lt;/p&gt; &lt;p&gt;content&lt;/p&gt; &lt;h3 id="faqc"&gt;title&lt;/h3&gt; &lt;p&gt;content&lt;/p&gt; &lt;p&gt;content&lt;/p&gt; &lt;p&gt;content&lt;/p&gt; &lt;p&gt;content&lt;/p&gt; &lt;h3 id="faqd"&gt;title&lt;/h3&gt; &lt;p&gt;content&lt;/p&gt; &lt;/div&gt; </code></pre> <p>And I want...</p> <pre><code>&lt;div id="container"&gt; &lt;h3 id="faqa"&gt;title&lt;/h3&gt; &lt;p&gt;content&lt;/p&gt; &lt;p align="right"&gt;&lt;a href="#"&gt;back to top&lt;/a&gt; &lt;h3 id="faqb"&gt;title&lt;/h3&gt; &lt;p&gt;content&lt;/p&gt; &lt;p&gt;content&lt;/p&gt; &lt;p align="right"&gt;&lt;a href="#"&gt;back to top&lt;/a&gt; &lt;h3 id="faqc"&gt;title&lt;/h3&gt; &lt;p&gt;content&lt;/p&gt; &lt;p&gt;content&lt;/p&gt; &lt;p&gt;content&lt;/p&gt; &lt;p&gt;content&lt;/p&gt; &lt;p align="right"&gt;&lt;a href="#"&gt;back to top&lt;/a&gt; &lt;h3 id="faqd"&gt;title&lt;/h3&gt; &lt;p&gt;content&lt;/p&gt; &lt;p align="right"&gt;&lt;a href="#"&gt;back to top&lt;/a&gt; &lt;/div&gt; </code></pre> <p>Here is my honest effort so far, but no answer as of yet...</p> <pre><code>$("#container").each (function() { if($(this).find('h3[id*="faq"]')){ var $mpage = window.location.pathname; $(this).find("p:last").append('&lt;p align="right"&gt;&lt;a href="'+$mpage+'"&gt;Back to top&lt;/a&gt; &lt;/p&gt;'); } }); </code></pre> <p>The jQuery API has an excellent guide on prepend and append, but neither of them help me in this specific case. Thanks for any light on the situation, I got about 40 of these anchors I have to add ;(</p> <p>Thanks again for any help!</p>
 

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