Skip to content

Instantly share code, notes, and snippets.

@BiancaNL
Created October 23, 2021 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BiancaNL/dee961b65620a5b37037d4b43f08b48f to your computer and use it in GitHub Desktop.
Save BiancaNL/dee961b65620a5b37037d4b43f08b48f to your computer and use it in GitHub Desktop.
<script>
// Get ToC div
toc = document.getElementById("ToC");
//Add a header
tocHeader = document.createElement("h2");
tocHeader.innerText="Table of contents";
toc.appendChild(tocHeader);
// Create a list for the ToC entries
tocList = document.createElement("ul");
// Get the h3 tags - ToC entries
headers = document.getElementsByTagName("h3");
// For each h3
for (i = 0; i < headers.length; i++){
// Create an id
name = "h"+i;
headers[i].id=name;
// a list item for the entry
tocListItem = document.createElement("li");
// a link for the h3
tocEntry = document.createElement("a");
tocEntry.setAttribute("href","#"+name);
tocEntry.innerText=headers[i].innerText;
tocListItem.appendChild(tocEntry);
tocList.appendChild(tocListItem);
}
toc.appendChild(tocList);
</script>
@BiancaNL
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment