TOC Building And Rebuilding

The text used for the headings is provided by a callback function. The callback is passed the heading jQuery object, which it can then use as a context for finding other content. In this example, we know that there is an 'Author' element after some of the headings so we pick that up and append it to the heading text.

Note that the fourth novel is missing its author because the span is missing the CSS class.

        var tocCallback = function ($elem) {
            var text = "'" + jQuery.trim($elem.text()) + "'";
            var author = $('.Author', $elem.parent()).text();
            if (author.length > 0) {
                text = text + ', by ' + author;
            }
            return text;
        }

        $(document).ready(function () {
            $('#tocHeadings').tocBuilder({ type: 'headings', textCallback: tocCallback });
        });
        

St Petersburg

Author: Andrei Bely

Ulysses

Author: James Joyce

Egyptian Stamp

Author: Osip Mandelstam

Topology of a Phantom City

Author: Alain Robbe-Grillet, but missing CSS class 'Author'