{"id":42088,"date":"2018-12-04T22:01:20","date_gmt":"2018-12-04T14:01:20","guid":{"rendered":"https:\/\/www.hongkiat.com\/blog\/?p=42088"},"modified":"2022-07-15T17:01:25","modified_gmt":"2022-07-15T09:01:25","slug":"create-auto-generated-table-of-contents-html-slot","status":"publish","type":"post","link":"https:\/\/www.hongkiat.com\/blog\/create-auto-generated-table-of-contents-html-slot\/","title":{"rendered":"How to Auto-generated Table of Contents with HTML Slots"},"content":{"rendered":"<p><strong>Table of contents<\/strong> can greatly improve the user experience of many websites, for instance <strong>documentation sites<\/strong> or <strong>online encyclopedias<\/strong> like Wikipedia. A well-designed table of contents <strong>gives an overview of the page<\/strong> and helps users quickly navigate to the section they are interested in.<\/p>\n<p>Traditionally, you can create table of contents either in HTML or with JavaScript, but the lately standardized HTML slots provide a <strong>middle way between the two<\/strong>. <strong><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTML\/Element\/slot\" target=\"_blank\" rel=\"noopener\">HTML Slot<\/a><\/strong> is a web standard that allows you to <strong>add placeholders to a web page<\/strong> and later <strong>fill it with content dynamically<\/strong>.<\/p>\n<p class=\"note\"><strong>Read Also:<\/strong> <a target=\"_blank\" href=\"https:\/\/www.hongkiat.com\/blog\/html-template-slow-tag-shadow-dom\/\" rel=\"noopener\">How to Use HTML &lt;template&gt; & &lt;slot&gt; With Shadow DOM<\/a><\/p>\n<h2>When to use the <code>&lt;slot&gt;<\/code> tag<\/h2>\n<p>You can place <code>&lt;slot&gt;<\/code> tags into the table of contents inside your HTML file, so the slots later can be <strong>filled with the relevant headings and subheadings<\/strong>. When the headings are changed the <strong>slots are auto-updated<\/strong>.<\/p>\n<p>With this technique, you need to create the HTML source code of the table of contents manually. JavaScript only auto-generates the text content of the table of contents, <strong>based on the headings or subheadings on the page<\/strong>.<\/p>\n<p>If you don\u2019t want to table of contents to be present in the HTML you need to <strong>generate both the layout and the content with JavaScript<\/strong>.<\/p>\n<h2>1. Create the HTML<\/h2>\n<p>The HTML source code for the TOC (table of contents) will be <strong>inside a <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTML\/Element\/template\" target=\"_blank\" rel=\"noopener\"><code>&lt;template&gt;<\/code><\/a> tag<\/strong>. The code inside <code>&lt;template&gt;<\/code> doesn\u2019t get rendered until it\u2019s added to the document by JavaScript. Our TOC will have <strong>placeholders<\/strong>, held in <code>&lt;slot&gt;<\/code> tags, <strong>for all the headings and subheadings<\/strong> found in the document.<\/p>\n<p>The <code>name<\/code> attribute of each <code>&lt;slot&gt;<\/code> will have the same value as the <code>slot<\/code> attribute in their corresponding headings and subheadings in the document.<\/p>\n<p>Below, you can see a <strong>sample HTML <code>&lt;article&gt;<\/code> with some headings and subheadings<\/strong>. The <code>&lt;div&gt;<\/code> at the beginning is where we\u2019ll insert the auto-filled TOC.<\/p>\n<pre>\r\n&lt;div id='toc'&gt;&lt;\/div&gt;\r\n&lt;article&gt;\r\n&lt;p&gt;Velociraptor (meaning \"swift seizer\" in Latin) is a \u2026&lt;\/p&gt;\r\n\r\n&lt;h2 slot='h-1'&gt;Description&lt;\/h2&gt;\r\n&lt;p&gt;Velociraptor was a mid-sized dromaeosaurid, with adults \u2026&lt;\/p&gt;\r\n\r\n\t&lt;h3 slot='sh-1-1'&gt;Feathers&lt;\/h3&gt;\r\n\t&lt;p&gt;Fossils of dromaeosaurids more primitive than \u2026&lt;\/p &gt;\r\n\r\n&lt;h2 slot='h-2'&gt;History of discovery&lt;\/h2&gt;\r\n&lt;p&gt;During an American Museum of Natural History expedition \u2026&lt;\/p&gt;\r\n\r\n&lt;h2 slot='h-3'&gt;Classification&lt;\/h2&gt;\r\n&lt;p&gt;Velociraptor is a member of the group Eudromaeosauria, a derived sub-group of \u2026&lt;\/p&gt;\r\n\r\n&lt;h2 slot='h-4'&gt;Paleobiology&lt;\/h2&gt;\r\n&lt;p&gt;The \"Fighting Dinosaurs\" specimen, found in 1971, preserves a \u2026&lt;\/p&gt;\r\n\r\n\t&lt;h3 slot='sh-4-1'&gt;Scavenging behavior&lt;\/h3&gt;\r\n\t&lt;p&gt;In 2010, Hone and colleagues published a paper on \u2026&lt;\/p&gt;\r\n\r\n\t&lt;h3 slot='sh-4-2'&gt;Metabolism&lt;\/h3&gt;\r\n\t&lt;p&gt;Velociraptor was warm-blooded to some degree, as it required a \u2026&lt;\/p&gt;\r\n\r\n\t&lt;h3 slot='sh-4-3'&gt;Pathology&lt;\/h3&gt;\r\n\t&lt;p&gt;One Velociratoptor mongoliensis skull bears two parallel \u2026&lt;\/p&gt;\r\n&lt;\/article&gt;<\/pre>\n<p>As you can see, each heading is <strong>given a unique <code>slot<\/code> value<\/strong>.<\/p>\n<p>And, here\u2019s the <strong>HTML code of the TOC<\/strong>, inside a <code>&lt;template&gt;<\/code> tag.<\/p>\n<pre>\r\n&lt;template&gt;\r\n&lt;ul&gt;\r\n\t&lt;li&gt;\r\n\t\t&lt;slot name='h-1'&gt;&lt;\/slot&gt;\r\n\t\t&lt;ul&gt;\r\n\t\t\t&lt;li&gt;&lt;slot name='sh-1-1'&gt;&lt;\/slot&gt;&lt;\/li&gt;\r\n\t\t&lt;\/ul&gt;\r\n\t&lt;\/li&gt;\r\n\t&lt;li&gt;&lt;slot name='h-2'&gt;&lt;\/slot&gt;&lt;\/li&gt;\r\n\t&lt;li&gt;&lt;slot name='h-3'&gt;&lt;\/slot&gt;&lt;\/li&gt;\r\n\t&lt;li&gt;\r\n\t\t&lt;slot name='h-4'&gt;&lt;\/slot&gt;\r\n\t\t&lt;ul&gt;\r\n\t\t\t&lt;li&gt;&lt;slot name='sh-4-1'&gt;&lt;\/slot&gt;&lt;\/li&gt;\r\n\t\t\t&lt;li&gt;&lt;slot name='sh-4-2'&gt;&lt;\/slot&gt;&lt;\/li&gt;\r\n\t\t\t&lt;li&gt;&lt;slot name='sh-4-3'&gt;&lt;\/slot&gt;&lt;\/li&gt;\r\n\t\t&lt;\/ul&gt;\r\n\t&lt;\/li&gt;\r\n&lt;\/ul&gt;\r\n&lt;style&gt;\r\n\tul {\r\n\t\tlist-style: none;\r\n\t}\r\n\/* \u2026 *\/\r\n&lt;\/style&gt;\r\n&lt;\/template&gt;\r\n<\/pre>\n<p>In the two code snippets above, notice the <strong>matching <code>slot<\/code> and <code>name<\/code> attributes<\/strong> inside the headings and the <code>&lt;slot&gt;<\/code> tags.<\/p>\n<h2>2. Number the headings<\/h2>\n<p>Before looking into the JavaScript\tcode that will add the TOC from the <code>&lt;template&gt;<\/code> to the document, let\u2019s <strong>add serial numbers for the headings, using <a href=\"https:\/\/www.hongkiat.com\/blog\/css-automatic-numbering\/\">CSS counters<\/a><\/strong>.<\/p>\n<pre>\r\narticle {\r\n\tcounter-reset: heading;\r\n}\r\narticle h2::before {\r\n\tcounter-increment: heading;\r\n\tcontent: '0'counter(heading)': ';\r\n}\r\n<\/pre>\n<p>Ensure that the <code>counter-reset<\/code> rule belongs to the element that\u2019s the <strong>immediate parent of all the titles carrying the <code>slot<\/code> attribute<\/strong> (which is the <code>&lt;article&gt;<\/code> element in our code).<\/p>\n<h2>3. Insert the TOC into the document<\/h2>\n<p>Now, we add the script that <strong>inserts the TOC above the <code>&lt;article&gt;<\/code> tag<\/strong>, inside the <code>&lt;div id='toc'&gt;&lt;\/div&gt;<\/code> container.<\/p>\n<pre>\r\ntemplateContent = document.querySelector('template').content;\r\narticle = document.querySelector('article').cloneNode(true);\r\narticle.attachShadow({  mode: 'closed' }).appendChild(templateContent.cloneNode(true));\r\ndocument.querySelector('#toc').appendChild(article);\r\n<\/pre>\n<p>The code snippet above <strong>creates a copy of <code>&lt;article&gt;<\/code><\/strong> and <strong>attaches a Shadow DOM Tree to it<\/strong>. We also <strong>add a copy of <code>&lt;template&gt;<\/code>\u2018s content<\/strong> to this Shadow DOM tree.<\/p>\n<p>Then, the cloned <code>&lt;article&gt;<\/code> is inserted into the <code>&lt;div id='toc'&gt;<\/code> element. The <code>&lt;article&gt;<\/code> element is <strong>now present in the TOC as well<\/strong>, however only its headings and subheadings that found a placeholder inside the TOC are visible.<\/p>\n<p>If we would reset the CSS counter at the <code>body<\/code> or <code>html<\/code> element instead of <code>article<\/code>, the counter would have counted the list of headings inside the TOC as well. That\u2019s why you should <strong>reset the counters at the immediate parent of the headings<\/strong>.<\/p>\n<p>Here is the screenshot of the output:<\/p>\n<figure><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/assets.hongkiat.com\/uploads\/create-auto-generated-table-of-contents-html-slot\/toc.jpg\" width=\"800\" height=\"687\" alt=\"Auto-generated table of contents\"><\/figure>\n<h2>4. Add hyperlinks<\/h2>\n<p>If you want to <strong>link the TOC titles to their respective headings and subheadings<\/strong> by adding <code>id<\/code> to the headings and anchoring their corresponding TOC text you will have to <strong>remove the repetitive <code>id<\/code> values from the cloned <code>article<\/code><\/strong>.<\/p>\n<pre>\r\n&lt;div id='toc'&gt;&lt;\/div&gt;\r\n&lt;article&gt;\r\n&lt;p&gt;Velociraptor (meaning \"swift seizer\" in Latin) is a \u2026&lt;\/p&gt;\r\n\r\n&lt;h2 id='h-1' slot='h-1'&gt;Description&lt;\/h2&gt;\r\n&lt;p&gt;Velociraptor was a mid-sized dromaeosaurid, with adults \u2026&lt;\/p&gt;\r\n\r\n&lt;h3 id='sh-1-1' slot='sh-1-1'&gt;Feathers&lt;\/h3&gt;\r\n&lt;p&gt;Fossils of dromaeosaurids more primitive than \u2026&lt;\/p &gt;\r\n&lt;!-- ... --&gt;\r\n&lt;\/article&gt;\r\n<\/pre>\n<p>As you can see above, the <code>id<\/code> attribute is<strong> added to every heading and subheading in the article<\/strong>.<\/p>\n<p>And, the <strong>titles inside the table of contents are anchored<\/strong>:<\/p>\n<pre>\r\n&lt;template&gt;\r\n&lt;ul&gt;\r\n&lt;li&gt;\r\n\t\t&lt;a href='#h-1'&gt;&lt;slot name='h-1'&gt;&lt;\/slot&gt;&lt;\/a&gt;\r\n\t\t&lt;ul&gt;\r\n\t\t\t&lt;a href='#sh-1-1'&gt;&lt;li&gt;&lt;slot name='sh-1-1'&gt;&lt;\/slot&gt;&lt;\/li&gt;&lt;\/a&gt;\r\n\t\t&lt;\/ul&gt;\r\n&lt;\/li&gt;\r\n&lt;!-- ... --&gt;\r\n&lt;\/ul&gt;\r\n&lt;\/template&gt;\r\n<\/pre>\n<p>In the extra line above, all <code>id<\/code> attributes are <strong>removed from the cloned article<\/strong> <em>before<\/em> attaching the Shadow DOM tree to it.<\/p>\n<pre>\r\ntemplateContent = document.querySelector('template').content;\r\narticle = document.querySelector('article').cloneNode(true);\r\narticle.querySelectorAll('*[id]').forEach((ele)=&gt;{ele.removeAttribute('id')})\r\narticle.attachShadow({  mode: 'closed' }).appendChild(templateContent.cloneNode(true));\r\ndocument.querySelector('#toc').appendChild(article);\r\n<\/pre>\n<p>See the screenshot of the <strong>linked table of contents<\/strong> below:<\/p>\n<figure><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/assets.hongkiat.com\/uploads\/create-auto-generated-table-of-contents-html-slot\/toc-linked.jpg\" width=\"800\" height=\"687\" alt=\"Linked table of contents\"><\/figure>\n<h2>Github demo<\/h2>\n<p>You can check out, download, or fork the code used in this post from our <a href=\"https:\/\/github.com\/hongkiat\/html-slot-toc\/\" target=\"_blank\" rel=\"noopener\">Github Repo<\/a>.<\/p>\n<p class=\"note\"><strong>Read Also:<\/strong> <a target=\"_blank\" href=\"https:\/\/www.hongkiat.com\/blog\/make-bookmarklet-with-javascript\/\" rel=\"noopener\">How to Create a Text-Search Bookmarklet with JavaScript <\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>Table of contents can greatly improve the user experience of many websites, for instance documentation sites or online encyclopedias like Wikipedia. A well-designed table of contents gives an overview of the page and helps users quickly navigate to the section they are interested in. Traditionally, you can create table of contents either in HTML or&hellip;<\/p>\n","protected":false},"author":145,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3392],"tags":[506,511],"topic":[4520],"class_list":["entry-content","is-maxi"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v22.8 (Yoast SEO v27.6) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>How to Auto-generated Table of Contents with HTML Slots - Hongkiat<\/title>\n<meta name=\"description\" content=\"Table of contents can greatly improve the user experience of many websites, for instance documentation sites or online encyclopedias like Wikipedia. A\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.hongkiat.com\/blog\/create-auto-generated-table-of-contents-html-slot\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Auto-generated Table of Contents with HTML Slots\" \/>\n<meta property=\"og:description\" content=\"Table of contents can greatly improve the user experience of many websites, for instance documentation sites or online encyclopedias like Wikipedia. A\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.hongkiat.com\/blog\/create-auto-generated-table-of-contents-html-slot\/\" \/>\n<meta property=\"og:site_name\" content=\"Hongkiat\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/hongkiatcom\" \/>\n<meta property=\"article:published_time\" content=\"2018-12-04T14:01:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-07-15T09:01:25+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/assets.hongkiat.com\/uploads\/create-auto-generated-table-of-contents-html-slot\/toc.jpg\" \/>\n<meta name=\"author\" content=\"Preethi Ranjit\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:site\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Preethi Ranjit\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/create-auto-generated-table-of-contents-html-slot\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/create-auto-generated-table-of-contents-html-slot\\\/\"},\"author\":{\"name\":\"Preethi Ranjit\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/e981676afae36d1ff5feb75094950ab3\"},\"headline\":\"How to Auto-generated Table of Contents with HTML Slots\",\"datePublished\":\"2018-12-04T14:01:20+00:00\",\"dateModified\":\"2022-07-15T09:01:25+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/create-auto-generated-table-of-contents-html-slot\\\/\"},\"wordCount\":673,\"commentCount\":9,\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/create-auto-generated-table-of-contents-html-slot\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/create-auto-generated-table-of-contents-html-slot\\\/toc.jpg\",\"keywords\":[\"HTML\",\"Web Developers\"],\"articleSection\":[\"Coding\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/create-auto-generated-table-of-contents-html-slot\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/create-auto-generated-table-of-contents-html-slot\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/create-auto-generated-table-of-contents-html-slot\\\/\",\"name\":\"How to Auto-generated Table of Contents with HTML Slots - Hongkiat\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/create-auto-generated-table-of-contents-html-slot\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/create-auto-generated-table-of-contents-html-slot\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/create-auto-generated-table-of-contents-html-slot\\\/toc.jpg\",\"datePublished\":\"2018-12-04T14:01:20+00:00\",\"dateModified\":\"2022-07-15T09:01:25+00:00\",\"description\":\"Table of contents can greatly improve the user experience of many websites, for instance documentation sites or online encyclopedias like Wikipedia. A\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/create-auto-generated-table-of-contents-html-slot\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/create-auto-generated-table-of-contents-html-slot\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/create-auto-generated-table-of-contents-html-slot\\\/#primaryimage\",\"url\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/create-auto-generated-table-of-contents-html-slot\\\/toc.jpg\",\"contentUrl\":\"https:\\\/\\\/assets.hongkiat.com\\\/uploads\\\/create-auto-generated-table-of-contents-html-slot\\\/toc.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/create-auto-generated-table-of-contents-html-slot\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Auto-generated Table of Contents with HTML Slots\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"name\":\"Hongkiat\",\"description\":\"Tech and Design Tips\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\",\"name\":\"Hongkiat.com\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"contentUrl\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"width\":1200,\"height\":799,\"caption\":\"Hongkiat.com\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/hongkiatcom\",\"https:\\\/\\\/x.com\\\/hongkiat\",\"https:\\\/\\\/www.pinterest.com\\\/hongkiat\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/e981676afae36d1ff5feb75094950ab3\",\"name\":\"Preethi Ranjit\",\"description\":\"A .NET developer with a JavaScript background, Preethi is expert in front-end coding, JavaScript, HTML, and CSS.\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/author\\\/preethi\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to Auto-generated Table of Contents with HTML Slots - Hongkiat","description":"Table of contents can greatly improve the user experience of many websites, for instance documentation sites or online encyclopedias like Wikipedia. A","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.hongkiat.com\/blog\/create-auto-generated-table-of-contents-html-slot\/","og_locale":"en_US","og_type":"article","og_title":"How to Auto-generated Table of Contents with HTML Slots","og_description":"Table of contents can greatly improve the user experience of many websites, for instance documentation sites or online encyclopedias like Wikipedia. A","og_url":"https:\/\/www.hongkiat.com\/blog\/create-auto-generated-table-of-contents-html-slot\/","og_site_name":"Hongkiat","article_publisher":"https:\/\/www.facebook.com\/hongkiatcom","article_published_time":"2018-12-04T14:01:20+00:00","article_modified_time":"2022-07-15T09:01:25+00:00","og_image":[{"url":"https:\/\/assets.hongkiat.com\/uploads\/create-auto-generated-table-of-contents-html-slot\/toc.jpg","type":"","width":"","height":""}],"author":"Preethi Ranjit","twitter_card":"summary_large_image","twitter_creator":"@hongkiat","twitter_site":"@hongkiat","twitter_misc":{"Written by":"Preethi Ranjit","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.hongkiat.com\/blog\/create-auto-generated-table-of-contents-html-slot\/#article","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/create-auto-generated-table-of-contents-html-slot\/"},"author":{"name":"Preethi Ranjit","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/e981676afae36d1ff5feb75094950ab3"},"headline":"How to Auto-generated Table of Contents with HTML Slots","datePublished":"2018-12-04T14:01:20+00:00","dateModified":"2022-07-15T09:01:25+00:00","mainEntityOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/create-auto-generated-table-of-contents-html-slot\/"},"wordCount":673,"commentCount":9,"publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/create-auto-generated-table-of-contents-html-slot\/#primaryimage"},"thumbnailUrl":"https:\/\/assets.hongkiat.com\/uploads\/create-auto-generated-table-of-contents-html-slot\/toc.jpg","keywords":["HTML","Web Developers"],"articleSection":["Coding"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.hongkiat.com\/blog\/create-auto-generated-table-of-contents-html-slot\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.hongkiat.com\/blog\/create-auto-generated-table-of-contents-html-slot\/","url":"https:\/\/www.hongkiat.com\/blog\/create-auto-generated-table-of-contents-html-slot\/","name":"How to Auto-generated Table of Contents with HTML Slots - Hongkiat","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/create-auto-generated-table-of-contents-html-slot\/#primaryimage"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/create-auto-generated-table-of-contents-html-slot\/#primaryimage"},"thumbnailUrl":"https:\/\/assets.hongkiat.com\/uploads\/create-auto-generated-table-of-contents-html-slot\/toc.jpg","datePublished":"2018-12-04T14:01:20+00:00","dateModified":"2022-07-15T09:01:25+00:00","description":"Table of contents can greatly improve the user experience of many websites, for instance documentation sites or online encyclopedias like Wikipedia. A","breadcrumb":{"@id":"https:\/\/www.hongkiat.com\/blog\/create-auto-generated-table-of-contents-html-slot\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.hongkiat.com\/blog\/create-auto-generated-table-of-contents-html-slot\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hongkiat.com\/blog\/create-auto-generated-table-of-contents-html-slot\/#primaryimage","url":"https:\/\/assets.hongkiat.com\/uploads\/create-auto-generated-table-of-contents-html-slot\/toc.jpg","contentUrl":"https:\/\/assets.hongkiat.com\/uploads\/create-auto-generated-table-of-contents-html-slot\/toc.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/www.hongkiat.com\/blog\/create-auto-generated-table-of-contents-html-slot\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.hongkiat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to Auto-generated Table of Contents with HTML Slots"}]},{"@type":"WebSite","@id":"https:\/\/www.hongkiat.com\/blog\/#website","url":"https:\/\/www.hongkiat.com\/blog\/","name":"Hongkiat","description":"Tech and Design Tips","publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.hongkiat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.hongkiat.com\/blog\/#organization","name":"Hongkiat.com","url":"https:\/\/www.hongkiat.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","contentUrl":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","width":1200,"height":799,"caption":"Hongkiat.com"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/hongkiatcom","https:\/\/x.com\/hongkiat","https:\/\/www.pinterest.com\/hongkiat\/"]},{"@type":"Person","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/e981676afae36d1ff5feb75094950ab3","name":"Preethi Ranjit","description":"A .NET developer with a JavaScript background, Preethi is expert in front-end coding, JavaScript, HTML, and CSS.","url":"https:\/\/www.hongkiat.com\/blog\/author\/preethi\/"}]}},"jetpack_featured_media_url":"https:\/\/","jetpack_shortlink":"https:\/\/wp.me\/p4uxU-aWQ","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/42088","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/users\/145"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/comments?post=42088"}],"version-history":[{"count":3,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/42088\/revisions"}],"predecessor-version":[{"id":60458,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/42088\/revisions\/60458"}],"wp:attachment":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/media?parent=42088"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/categories?post=42088"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/tags?post=42088"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/topic?post=42088"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}