-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoc.js
More file actions
33 lines (25 loc) · 870 Bytes
/
toc.js
File metadata and controls
33 lines (25 loc) · 870 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Get ToC div
toc = document.getElementById( "ToC" );
//Add a header
tocHeader = document.createElement( "h6" );
tocHeader.innerText = "Contents";
toc.appendChild( tocHeader );
// Create a list for the ToC entries
tocList = document.createElement( "ul" );
// Get the h2 tags - ToC entries
headers = document.getElementsByTagName( "h2" );
// 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 );