score:3

Accepted answer

You can do it like this:

//here chart is the div on which you want to attach the ul and li.
var myDiv = d3.select('#chart')/
.append("ul")//root ul
.append("li")//root li
.text(data.name)//root li name
.append("ul");//to this add ul


//note i am iterating through the data via recursion.
function makeElements(parentDOM, myData){
    myData.children.forEach(function(child){
    //add li element
    parentDOM.append("li").text(child.name);
    //if children then make ul
    if (child.children.length > 0){
        var ul = parentDOM.append("ul");
      //recurse pass ul as parentDOM
      makeElements(ul, child);
    } 
  });
}

Working code here


Related Query