JSFiddle - React, Tailwind, and code Playground

HTML

<h2>a test</h2>
<h2>another test</h2>
<ul class="chapters_list"></ul>

JavaScript

$('h2').each(function() {
    var text = $(this).text();
    $('<li/>')
        .append($('<a />', {text: text, href:'#'+text.replace(/\s/g, '-')}))
        .appendTo('ul.chapters_list');
});