Hierarchical Table Of Contents Plugin For jQuery - jquery-toc.js

File Size: 5.58 KB
Views Total: 1172
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
Hierarchical Table Of Contents Plugin For jQuery - jquery-toc.js

Yet another jQuery plugin for generating a hierarchical table of contents from headings within a specified container to improve the user experience on your content-heavy webpage.

The plugin generates navigation links in nested HTML lists that enables the user to quickly navigate between content sections on your long webpage.

To enable the TOC to smoothly scroll through your webpage, you might need a smooth scroll plugin.

How to use it:

1. Create an anchor link to toggle the table of contents.

<a id="toc">Open TOC</a>

2. Add unique IDs to the heading elements. If not specified, the plugin will automatically add 'toc-x' to the headings.

<h1 id="first">H1 Element</h1>
<h2 id="last">Last Heading</h2>

3. Place jQuery JavaScript library and the jquery-toc.js script at the bottom of the webpage.

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" 
<script src="jquery-toc.js"></script>

4. Call the function on the toc toggle link and done.


5. Specify the target container where the plugin looks for all the heading elements. Default: the whole body.

  container: $('.container')

6. Specify the heading elements. Default: 'h1, h2, h3'.

  selectors: 'h1, h2, h3, h4, h5'

7. Decide whether or not to collapse the table of contents on init.

  status: true // expand the TOC on init

8. Available callback functions whihc can be used to activate the toggle link.

  onOpen: function(list) {
    var $html = $('<div class="toc-wrapper"><p class="toc-title">Table of contents</p><div class="toc-list"></div></div>');


    $(this).text('Close TOC');
  onClose: function() {
    $(this).text('Open TOC');

9. Specify the list type you want to use.

  listTag: 'ul' // default: ol



  • Allows to use ordered or unordered list.
  • Handling xss

This awesome jQuery plugin is developed by biallo. For more Advanced Usages, please check the demo page or visit the official website.