Creating a Table of Contents with JavaScript for Your Webpage

In this blog post, I'll walk you through how-to create a Table of Contents for your web page using JavaScript.

Creating a Table of Contents with JavaScript for Your Webpage
Photo by Aaron Burden / Unsplash

When it comes to enhancing user experience and improving the navigation of long web pages, one handy feature you can add is a Table of Contents (TOC). A TOC provides readers with an easy way to jump to different sections of your content quickly. In this blog post, I'll walk you through how-to create a Table of Contents for your web page using JavaScript.

Why a Table of Contents?

A Table of Contents is especially useful for lengthy articles, tutorials, or documentation. It allows your readers to:

  1. Navigate Easily: Quickly find and jump to the section they are interested in.
  2. Understand Structure: Get an overview of the content's structure and hierarchy.
  3. Improved User Experience: Enhance user experience by providing a helpful navigation aid.

The JavaScript Code

To create a Table of Contents dynamically based on headings in your HTML content, you can use JavaScript. Below is the JavaScript code you can use:

document.addEventListener("DOMContentLoaded", function () {
    const headings = document.querySelectorAll("h1[id], h2[id], h3[id], h4[id], h5[id], h6[id]");
    
    if (headings.length > 0) {
        const tocContainer = document.createElement("div");
        tocContainer.className = "toc-container";

        const tocHeading = document.createElement("h2");
        tocHeading.textContent = "Table of Contents";
        tocContainer.appendChild(tocHeading);

        const tocList = document.createElement("ul");
        tocContainer.appendChild(tocList);

        headings.forEach((heading) => {
            const tocItem = document.createElement("li");
            const tocLink = document.createElement("a");
            tocLink.textContent = heading.textContent;
            tocLink.href = "#" + heading.id;
            tocItem.appendChild(tocLink);
            tocList.appendChild(tocItem);
        });

        document.body.insertBefore(tocContainer, document.body.firstChild);
    }
});

How It Works

  1. DOMContentLoaded Event: The code begins by listening for the DOMContentLoaded event, ensuring that the script runs once the document is fully loaded.
  2. Selecting Headings: It selects all the headings (h1, h2, h3, h4, h5, and h6) in the HTML content that have id attributes.
  3. Creating the TOC: If there are headings with id attributes, the code creates a TOC container, heading, and an unordered list.
  4. Looping Through Headings: For each heading, it creates a list item (li) and a link (a) with the heading text and a link to the corresponding section using the href attribute.
  5. Inserting TOC: Finally, the TOC is inserted at the beginning of the body element.

Customization

You can customize the appearance of the TOC by adding CSS styles to your liking. The provided code generates a simple TOC with links, but you can modify it to match the design of your website.

Download the JavaScript, and sample HTML and CSS files on Gumroad.

Adding a Table of Contents to your web page can greatly improve the user experience, making it easier for readers to navigate your content. With this JavaScript code, you can dynamically generate a TOC based on your headings, enhancing the accessibility and usability of your website. Try it out on your long-form articles or documentation to provide a more user-friendly reading experience.