treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[jquery] Trying to create a dynamic TOC nested list

  • Hey fellow css and jquery fans,

    (i hope it's okay to post this here as i saw other jquery threads around. Maybe there should be seperate jquery forum?)

    anyway:
    i am trying to pull all headings out of a page and create a unordered
    list in the sidebar, which should then act as navigation help.

    $(document).ready(function() {
    $('#sidebar #references').after(\"<ul class='subnav'>\");
    $('.main h3.caps a').each(function(i, val) {
    $('ul.subnav').append(this.firstChild);
    });

    });


    I am able to append the anchors, but i can't create
    the list. I was looking at the wrap-function of jquery, but didn't
    figure out how to use it in this scenario. Also, i am actually "pulling out" the anchors of the main page so they are "cut & pasted" in the sidebar. Of course, i only want to copy their values..

    Can somebody explain me how to get the copy-value of this.firstChild
    sorrounded by "<li></li>"?


    aah and here is my html markup:
    Main-content:
    ==========
    <div class=\".main\">
    <h3 class=\"caps\"><a name=\"Auslandspraktika\">Auslandspraktika</a></h3>
    <h3 class=\"caps\"><a name=\"AnotherAnchor\">AnotherAnchor</a></h3>
    </div>


    Sidebar:
    =======
    <div id=\"sidebar\">
    <ul>
    <li><a id=\"index\" href=\"/index.html\">Home</a></li>
    <li><a id=\"references\" href=\"/references.html\">References</a></li>
    <li><a id=\"contact\" href=\"/contact.html\">Contact</a></li>
    </ul>
    </div>


    Thanks in advance,
    Michael