In the part, we discuss how to build a table of content with script and the function `target-counter()`.
All the files (scripts, CSS, HTML exemples) of this part to create a table of content are available on gitlab: [https://gitlab.pagedmedia.org/tools/experiments/tree/master/table-of-content](https://gitlab.pagedmedia.org/tools/experiments/tree/master/table-of-content).
## Build the element of the table of content
First, to have a table of content, you need to make HTML lists of the reveleant titles of your document and link to the unique identifier of this element. This part can be done with your own tool/generator but here is an exemple of a script to generate a table of content:
Copy this script to a `.js` and link this file to your document.
The table of content need to be generated before that paged.js fragmented the content into pages. It's important. So, you need to register the handler `beforeParsed()` and call the table of content script inside.
Add this code in the `head` of you html document after the paged.js script:
```html
<script>
classhandlersextendsPaged.Handler{
constructor(chunker,polisher,caller){
super(chunker,polisher,caller);
}
beforeParsed(content){
createToc({
content:content,
tocElement:'#my-toc-content',
titleElements:['.mw-content-ltr h2','h3']
});
}
}
Paged.registerHandlers(handlers);
</script>
```
**Configuring the script**
`tocElement`: define the id element where the toc list will be create
`titleElements`: array of the title element you want in the toc list. You can add as many as you want and the elements can be classes like `.title-1` or `.my-content h1`
## Generate page numbers
To generate the page numbers of the table of content, use the links to the target anchors titles in the book and the CSS function `target-counter()`.
In your document, each title that appears in the table of content have a special identifier:
In the CSS, the `target-counter` property is used in `::before` and `::after` pseudo-elements and set into the `content` property:
```CSS
#toc li a::after{
content: target-counter(attr(href), page);
float: right;
}
```
## Style the table of content
Maybe you want some style, counter list and leaders for your table of content. Based on the table of content generated with above, it's an exemple of CSS you can use :
```css
#list-toc-generated{list-style:none;}
#list-toc-generated.toc-elementa::after{
content:" p. "target-counter(attr(href),page);
float:right;
}
#list-toc-generated.toc-element-level-1{
margin-top:25px;
font-weight:bold;
}
#list-toc-generated.toc-element-level-2{
margin-left:25px;
}
/* target-text(attr(href), before) doesn't work for now, replace with counters (see below)*/