Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6608871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:39:20+00:00 2026-05-25T19:39:20+00:00

Looking for a great way to add anchor links within my HTML document after

  • 0

Looking for a great way to add anchor links within my HTML document after my pattern of H3 and the last p element block.

This is my original HTML

<div id="container">
    <h3 id="faqa">title</h3>
    <p>content</p>

    <h3 id="faqb">title</h3>
    <p>content</p>
    <p>content</p>

    <h3 id="faqc">title</h3>
    <p>content</p>
    <p>content</p>
    <p>content</p>
    <p>content</p>

    <h3 id="faqd">title</h3>
    <p>content</p>
</div>

And I want…

<div id="container">
    <h3 id="faqa">title</h3>
    <p>content</p>
    <p align="right"><a href="#">back to top</a>

    <h3 id="faqb">title</h3>
    <p>content</p>
    <p>content</p>
    <p align="right"><a href="#">back to top</a>

    <h3 id="faqc">title</h3>
    <p>content</p>
    <p>content</p>
    <p>content</p>
    <p>content</p>
    <p align="right"><a href="#">back to top</a>

    <h3 id="faqd">title</h3>
    <p>content</p>
    <p align="right"><a href="#">back to top</a>

</div>

Here is my honest effort so far, but no answer as of yet…

$("#container").each (function() {
     if($(this).find('h3[id*="faq"]')){
     var $mpage = window.location.pathname;
     $(this).find("p:last").append('<p align="right"><a href="'+$mpage+'">Back to top</a>
</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 ;(

Thanks again for any help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:39:21+00:00Added an answer on May 25, 2026 at 7:39 pm
    var link = '<p align="right"><a href="#">back to top</a></p>';
    $('#container h3').each(function() {
        $(this).nextUntil('h3', 'p').last().after(link);
    });
    

    should do the trick.

    DEMO

    Update: karim’s answer is nice and short and you should go with this one Edit: Apparently, as nice as the answer seems, it does not add a link to the last paragraph of the last heading.

    I will still leave this answer here, as it shows a different way of approaching the problem.

    karim’s mindset basically is: Take every h3 and add the link if it was preceded by a paragraph.

    Mine was: Take every h3 and find the last following paragraph (before the next h3) and append the link.

    He thought upwards, I downwards 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This week I decided to add a new element to a javascript array by
I am trying to add an HTML link with an image as the anchor,
I've been looking (without great luck) for the perfect reference card with all the
A while back a found a great-looking framework that allowed .net developers to implement
I like the Stack Overflow comment UI a great deal and I'm looking into
I'm looking at all the CSS Drop shadow tutorials, which are great. Unfortunately, I
I'm looking for a generic way on managing database connections in Zend Framework (1.1).
I am looking to add a (local, not online) high scores table to my
I'm looking at using less . js (looks great), but our site requires that
Walter Rumsby provided a great answer to Where to place JavaScript in an HTML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.