Search code examples
javascriptjsontableofcontents

Table of Content javascript input


I want to create a simple table of content for my project. I tried using this library:

https://codepen.io/chriscoyier/pen/EnLwb/

and giving a sample json input without success.

this is my json:

"{"0":{"text":"text0","id":0},"1":{"text":"text1","id":1}}"

Problem

I'm trying to replace $("article h3") with

var = $(jsonInput) 

coming from HTML call. The result is blank - nothing is displayed.


Solution

  • I've found what I was missing:

    var dictionary = {
        "data":[
            {"id":"0","name":"ABC","text":"PQR"},
            {"id":"1","name":"DEF","text":"PQR"}
        ]
    };
    
    dictionary.data.forEach(function(item){
      el = $(item);
      title = el.attr("text");
      link = "#" + el.attr("id");
    

    So in order to replace html element of the code I needed: 1) to iterate over the json array 2) replace jquery function

       title = el.text(); 
    

    with

    el.attr("text");