Search code examples
javascripthtmlcssjscript.net

how to remove and create new ul li tag in html using javascript


Can anyone help me to get this work in d3js

My html tag are as below

<div id="source1"><ul><li>Source</li></ul></div>

i want to modify the <li>Source</li> with <li>some other text</li> by removing all the UL LI tags in DIV id source1 and creating new UL LI tags in div id source1

My code is here

d3.selectAll("ul").remove()

d3.selectAll("li").remove()

var test=document.getElementbyId('source1');
var ul=document.createElement('ul');
document.body.appendChild(test);
test.appendChild(ul);
var li=document.createElement('li');
ul.appendChild(li);
li.innerHTML="some other text";

Solution

  • here is code that should work :

    var test=document.getElementById('source1');
    var li=test.children[0].children[0];
    li.innerHTML="some other text";
    

    Instead of recreating element you can change text only.