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 7988851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:34:40+00:00 2026-06-04T12:34:40+00:00

I am building a script that generates a table of content (as ) of

  • 0

I am building a script that generates a table of content (as ) of the headings (currently only ) on a page.

I am having trouble with appending elements without an id, because I want to assign a href target to a link, and use a variable as the link’s content.

The code that I am working on:

        var h_num; // introduce the running id counter
        $("h2").each(function (i) {
            // retrieve the text contained in the element
            $h_content = $(this).text();
            // add a running id to the h2
            $(this).attr("id", ("h2_"+i));
            // the id attribute value that was just added to the element
            $h_num = ("h2_"+i);
            $new_id = ("#" + $h_num);
            console.log($h_num, "hoonum", $new_id, "newid"); //for debugging
            // append a <li> element with a link to the heading 
            $('#toc ol').append(
                '<li>' + '<a href="$h_num">$h_content'
            );
        });    

So I can’t find a way to escape the line inside the append function.

$h_content is the title of the heading (to be included as the text for the link to the heading), and $new_id is the href target id.

I would like the table of content’s markup to look like this

<ol>
    <li><a href="#h2_0">First heading topic</a></li>
    <li><a href="#h2_1">Second heading topic</a></li>
</ol>   
  • 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-06-04T12:34:42+00:00Added an answer on June 4, 2026 at 12:34 pm

    For performance purposes, you should append to a documentFragement first.

        var frag = document.createDocumentFragment();
        $("h2").each(function (i, el) {
            var li = document.createElement( "li" );
            var $a = $("<a>", { id: "h2_" + i }).text( $(el).text() );
            li.appendChild( $a[0] )
            frag.appendChild( li );
        });
        $("ol").append(frag);
    ​
    

    http://jsfiddle.net/landau/RyeAA/

    http://jsfiddle.net/landau/RyeAA/5/ – Updated version

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

Sidebar

Related Questions

I'm currently building my own login script and have noticed that most append the
I am having trouble building a Python function that launches TkInter objects, with commands
A php script is building the following (very complicated script that is too long
I've got a script that is capable of building up a longish transaction of
I'm currently building a CI build script for a legacy application. There are sporadic
My script is building a file that consists of bytes \xdd where dd -
I am currently building an application that requires the identification of an element by
I'm building a quiz web application that dynamically generates from a database on my
We have a series of scripts that are used when building Apache. Specifically these
I´m building a bash script and i want to replace ${PRODUCT_NAME} with a WORD

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.