css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <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>
<h2>Features</h2>
<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="https://codepen.io/lehollandaisvolant/pen/wJgGdR">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')
		containerLiNode.appendChild(ulNode);
		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'));
		link.appendChild(document.createTextNode(currentHeading.textContent))
		liNode.appendChild(link);
	} else {
		liNode.appendChild(document.createTextNode(currentHeading.textContent));
	}
	cursorNode.appendChild(liNode);
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console