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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:01:51+00:00 2026-05-10T19:01:51+00:00

I have headers in <h1> through <h6> tags. Is there a way that I

  • 0

I have headers in <h1> through <h6> tags. Is there a way that I can use JavaScript to generate a table of contents for the contents that serves as anchor tags as well?

I would like the output to be something like:

<ol>     <li>Header 1</li>     <li>Header 1</li>         <li>Header 2</li>             <li>Header 3</li> </ol> 

I am not currently using a JavaScript framework, but I don’t see why I couldn’t use one.

I am also looking for something done, since I’m guessing this is a common problem, but if not, a starting point to roll my own would be good.

  • 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. 2026-05-10T19:01:52+00:00Added an answer on May 10, 2026 at 7:01 pm

    I couldn’t resist putting together a quick implementation.

    Add the following script anywhere on your page:

    window.onload = function () {     var toc = '';     var level = 0;      document.getElementById('contents').innerHTML =         document.getElementById('contents').innerHTML.replace(             /<h([\d])>([^<]+)<\/h([\d])>/gi,             function (str, openLevel, titleText, closeLevel) {                 if (openLevel != closeLevel) {                     return str;                 }                  if (openLevel > level) {                     toc += (new Array(openLevel - level + 1)).join('<ul>');                 } else if (openLevel < level) {                     toc += (new Array(level - openLevel + 1)).join('</ul>');                 }                  level = parseInt(openLevel);                  var anchor = titleText.replace(/ /g, '_');                 toc += '<li><a href=\'#' + anchor + '\'>' + titleText                     + '</a></li>';                  return '<h' + openLevel + '><a name=\'' + anchor + '\'>'                     + titleText + '</a></h' + closeLevel + '>';             }         );      if (level) {         toc += (new Array(level + 1)).join('</ul>');     }      document.getElementById('toc').innerHTML += toc; }; 

    Your page should be structured something like this:

    <body>     <div id='toc'>         <h3>Table of Contents</h3>     </div>     <hr/>     <div id='contents'>         <h1>Fruits</h1>         <h2>Red Fruits</h2>         <h3>Apple</h3>         <h3>Raspberry</h3>         <h2>Orange Fruits</h2>         <h3>Orange</h3>         <h3>Tangerine</h3>         <h1>Vegetables</h1>         <h2>Vegetables Which Are Actually Fruits</h2>         <h3>Tomato</h3>         <h3>Eggplant</h3>     </div> </body> 

    You can see it in action at https://codepen.io/scheinercc/pen/KEowRK (old link: http://magnetiq.com/exports/toc.htm (Works in IE, FF, Safari, Opera))

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

Sidebar

Ask A Question

Stats

  • Questions 69k
  • Answers 69k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer You can use the ']}' command. You may have to… May 11, 2026 at 12:40 pm
  • added an answer Turns out what happens is the DockPanel in the DetailsTemplate… May 11, 2026 at 12:40 pm
  • added an answer You might want to take a look at classes that… May 11, 2026 at 12:40 pm

Related Questions

I have a Spring Interceptor which attempts to add an HTTP header in the
Lets say that I have a header user control in a master page, and
Suppose I have #define foo in various header files. It may expand to some
I have a series of divs in a pattern of header/body, where a click
I'm trying to write some PHP to upload a file to a folder on
I am trying to have a table header, in a seperate div stay in
I have an AJAX application which has used CSS divs to create panels on
I have a header <h1>My Header</h1> It has a height of 150px and a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.