Q:

jQuery table of contents plugin that uses anchor names

I'm looking for a jQuery table of contents plugin that uses given anchor names. The ones that I've found always generate they own anchor names leading to instable URIs like somewhere#toc_1_4. But for an anchor like this
<h2><a name="something-special">Something Special</a></h2>

I want an URI like somewhere#something-special.
Is there such a plugin?

A:

I don't know such plugin but this can be easily done by yourself:
$('a[name]').each(function(i, v) {
    var e = $(v);
    if (e.text() != '') {
        $('#toc').append('<li><a href="#' + e.attr('name') + '">' + e.text() + '</a></li>');
    }
});

For each link, append a list item to your #toc (could be an ordered list):
<h1>Table of contents:</h1>
<ol id="toc"></ol>

You could also add something like:
<a name="something-special" data-toc="yes" data-title="Special topic">special</a>

and use the code:
$('a[name]').each(function(i, v) {
    var e = $(v);
    var t = e.data('title') || e.text();
    if (e.data('toc') == 'yes' && t.length > 0) {
        $('#toc').append('<li><a href="#' + e.attr('name') + '">' + t + '</a></li>');
    }
});

Here is a jsfiddle for you: http://jsfiddle.net/27Ubt/1/

A:

Consider this plugin:

Table of Contents jQuery Plugin

Hope it helps.

