Pen Settings



CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource


Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource


Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.


                <p>Table of Contents :</p>
<ul id="toc"></ul>

<h1>Simple JavaScript TOC</h1>
<h2>How to use?</h2>
<p>Simply inclure your JS in your page and add a UL element with “toc” as ID. That’s it.</p>
<p>If your headings do have IDs, they will be detected and the TOC will contain internal links pointing to the headings in that page.</p>
<h2>Enhancements ?</h2>
<p>One could add some CSS enhancements, with the “counter” property. This will display automatic numbers to the headings, without you to add them manually in the HTML, so you won’t have to edit every title if one day you choose to add/remove a chapter.</p>
<p>I forked this “pen” and <a href="">added that very feature here</a>.</p>
<h2>Part 3</h2>
<p>Dolor sit amet…</p>
<h2>Part 4</h2>
<h3>Lorem Ipsum</h3>
<p>Lorem ipsum…</p>
<h3>P4 - Chapter 1</h3>
<h4>P4-C1 - section 1</h4>
<p>Lorem ipsum…</p>
<p>Dolor sit amet…</p>
<h4>P4-C1 - section 2</h4>
<p>Lorem ipsum…</p>
<p>Dolor sit amet…</p>
<h3>P4 - Chapter 2</h3>
<h4>P4-C2 - section 1</h4>
<p>Lorem ipsum…</p>
<p>Dolor sit amet…</p>
<h2>Part 5</h2>
<p>Lorem ipsum…</p>
<p>Dolor sit amet…</p>



                h1, h2, h3, h4 {
  text-align: center;

#toc {
  color: red;


                var TOC = document.getElementById('toc'),
headings = document.querySelectorAll('h1, h2, h3, h4, h5, h6'),
parentLevel = 1,
cursorNode = TOC;

for (var i = 0, len = headings.length ; i < len ; ++i) {
	var currentHeading = headings[i];
	var newLevel = parseInt(currentHeading.tagName.substr(1,1));
	var diff = newLevel - parentLevel;
	if (diff > 0) {
		var containerLiNode = cursorNode.lastChild;
		var ulNode = document.createElement('UL')
		cursorNode = ulNode;
		parentLevel = newLevel;		
	if (diff < 0) {
		while (0 !== diff++) {
			cursorNode = cursorNode.parentNode.parentNode;
		parentLevel = newLevel;

	var liNode = document.createElement('LI');
	if (currentHeading.hasAttribute('id')) {
		var link = document.createElement('A');
		link.setAttribute('href', '#' + currentHeading.getAttribute('id'));
	} else {