# Simple Table of Content (ToC) for pages

**URL:** <https://forum.ghost.org/t/simple-table-of-content-toc-for-pages/48473>\
**Category:** Using Ghost\
**Created:** [June 28, 2024, 9:47am UTC](https://forum.ghost.org/t/simple-table-of-content-toc-for-pages/48473 "2024-06-28T09:47:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ian\_R](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/ian_r/32/25877_2.png) [@Ian\_R](https://forum.ghost.org/u/Ian_R)\
**Post date:** [June 28, 2024, 9:47am UTC](https://forum.ghost.org/t/simple-table-of-content-toc-for-pages/48473/1 "2024-06-28T09:47:03Z")

</div>

I was trying to find a simple way to add a **table of content** to an individual **page** in Ghost

The following code works perfectly for me.

Just paste the code directly into the actual page (using the ‘HTML’ element) below your title and above where your content will start.

I have it set to just add H2 items, but I’m sure it can be adjusted. You can also edit some of the display settings as you wish.

I haven’t tried it on ‘posts’ (as I already have that installed in the theme)

Hopefully this is useful to someone

```auto
<div id="toc-placeholder">
    <script>
        window.addEventListener('DOMContentLoaded', function () {
            const tocContainer = document.createElement('div');
            tocContainer.id = 'toc';
            tocContainer.innerHTML = '<h2>Table of Contents</h2><ul>';

            const h2Elements = document.querySelectorAll('h2');
            h2Elements.forEach(h2 => {
                const listItem = document.createElement('li');
                const link = document.createElement('a');
                link.href = '#' + h2.id; // Ensure "#" before the ID
                link.textContent = h2.textContent;
                listItem.appendChild(link);
                tocContainer.querySelector('ul').appendChild(listItem);
            });

            const placeholder = document.getElementById('toc-placeholder');
            placeholder.parentNode.replaceChild(tocContainer, placeholder);
        });
    </script>

    <style>
        #toc {
            background-color: #f0f0f0;
            padding: 20px;
            border: 1px solid #ccc;
        }

        #toc ul {
            list-style-type: none;
            margin: 0;
            padding: 0;
        }

        #toc a {
            color: #333;
            text-decoration: none;
        }
    </style>
</div>

```

---

<div class="post-metadata">

**Author:** ![RMay](https://avatars.discourse-cdn.com/v4/letter/r/73ab20/32.png) [@RMay](https://forum.ghost.org/u/RMay)\
**Post date:** [July 2, 2024, 11:11am UTC](https://forum.ghost.org/t/simple-table-of-content-toc-for-pages/48473/2 "2024-07-02T11:11:36Z")

</div>

Cool, would this work for non-self hosted Ghost accounts?

---

<div class="post-metadata">

**Author:** ![jannis](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/jannis/32/39718_2.png) [@jannis](https://forum.ghost.org/u/jannis)\
**Post date:** [July 2, 2024, 1:36pm UTC](https://forum.ghost.org/t/simple-table-of-content-toc-for-pages/48473/3 "2024-07-02T13:36:04Z")

</div>

It should work, yes. Haven’t tried it myself, but what @Ian_R is proposing is simply pasting the code into a HTML card directly in the editor.

---

<div class="post-metadata">

**Author:** ![pureflo](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/pureflo/32/35694_2.png) [@pureflo](https://forum.ghost.org/u/pureflo)\
**Post date:** [July 2, 2024, 10:27pm UTC](https://forum.ghost.org/t/simple-table-of-content-toc-for-pages/48473/4 "2024-07-02T22:27:14Z")

</div>

Here’s one that I use - [GitHub - Trizone-media/toc: Place an on-page table of contents that dynamically changes based on headings](https://github.com/Trizone-media/toc)
