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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:35:35+00:00 2026-06-12T12:35:35+00:00

I want to generate a kind toc for a html document with jQuery –

  • 0

I want to generate a kind toc for a html document with jQuery – without
using any plugins. I’m just starting to learn/improve my jQuery know-how.

HTML input:

<div id="toc"></div>
<h3><a name="construction-block"></a>Construction Block</h3><p>...</p>
<h3><a name="construction-box"></a>Construction Box</h3><p>...</p>
...

JS:

$(document).ready(function(){
$("#toc").append('<p>table of contents:</p>');
$("h3").each(function(i) {
    var current = $(this);
    $("#toc").append("<h3>"+current.html()+"</h3>");
});
})

HTML output

<div id="toc">
<p>table of contents:</p>
<h3>
<a name="construction-block"></a>
Construction Block
</h3>
<h3>
<a name="construction-box"></a>
Construction Boxk
</h3>
</div>

It’s only partial success.

Desired HTML output

<div id="toc">
<p>table of contents:</p>
<h3>
<a href="#construction-block">Construction Block</a>
</h3>
<h3>
<a href="#construction-box">Construction Box</a>
</h3>
</div>
  • 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-12T12:35:38+00:00Added an answer on June 12, 2026 at 12:35 pm

    I’d suggest the following (given some changes in the HTML):

    $('<p />', {
        'text': 'Table of contents'
    }).appendTo('#toc');
    $('<ol />').appendTo('#toc');
    
    $('h3').each(
        function() {
            var that = this,
                $that = $(that),
                a = $('<a />', {
                    'href': '#' + that.id,
                    'text': $that.text()
                }),
                li = $('<li />').append(a).appendTo('#toc ol');
        });​
    

    This jQuery creates an ordered list (which is, essentially, a semantic definition of what a table of contents is), and appends the text of the h3 elements to a new a element within newly-created, and appended, list-items.

    The HTML changes are the removal of the <a> elements, since the id can (and should) be used instead of named anchors (and this also works in IE).

    The new HTML:

    <div id="toc"></div>
    <h3 id="construction-block">Construction Block</h3>
    <p>...</p>
    <h3 id="construction-box">Construction Box</h3>
    <p>...</p>
    

    JS Fiddle demo.

    I didn’t think of this until now, but you could achieve the same even without id attributes on the h3 elements, and simply use the jQuery to create the relevant id attributes as it goes:

    $('<p />', {
        'text': 'Table of contents'
    }).appendTo('#toc');
    $('<ol />').appendTo('#toc');
    
    $('h3').each(
        function(i,el) {
            var $that = $(el),
                text = $that.text(),
                id = text.toLowerCase().replace(/\s+/,'-');
            el.id = id;
            var a = $('<a />', {
                'href' : '#' + id,
                'text' : text
            }),
                li = $('<li />').append(a).appendTo('#toc ol');
        });​
    

    JS Fiddle demo.

    References:

    • append().
    • appendTo().
    • jQuery (how to create elements with jQuery).
    • Regular Expressions.
    • String.replace().
    • String.toLowerCase().
    • text().
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to generate an HTML table from a couple specified parameters. Specifically, the
I want to generate strong types based on weakly typed data sources, using the
I want to generate a LaTeX document from Objective-C and compile it with pdfLatex
I want to generate a dynamic site using Zend_Layout. My layout (/application/layouts/scripts/layout.phtml) contains the
I just want to know what is your opinion about how to fingerprint/verify html/links
I use XSLT to generate an HTML table using info from an XML file.
Let's say I have a class of 30 students and want generate every possible
I want to generate a list of files where the name consists of ${filename}.${date}
I want to generate a random timestamp and add a random increment to it
I want to generate random integers between 0-9 (inclusive on both ends), but I

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.