javascript - Is it really necessary to use array, to display an innerHTML of a complete node list after child remove? -


this code works when start remove nodes complete list disappear , i'm left 1 node in innerhtml, instead of removing 1 one gradually want. leave few none behind perhaps gradually instead of straight 1 displayed in innerhtml, of div displays removal sort of say... can work using arrays i'm curious if there's way out without using any(arrays)?

html code

<button onclick="remov()">remove</button> <button onclick="adddiv()">add div</button> <div> <div></div> <div></div><div></div> <div></div> </div> <div id="tt"></div> 

javascript code

var stage = document.getelementsbytagname("div")[0]; var tt = document.getelementbyid("tt");  function remov() {     if (stage.haschildnodes()) {         stage.removechild(stage.firstchild);         (var = 0; < stage.childnodes.length; i++) {             tt.innerhtml = stage.childnodes[i].nodename;         }          if (!stage.haschildnodes()) {             tt.innerhtml = "no nodes";         }     } }  (var = 0; < stage.childnodes.length; i++) {     tt.innerhtml += stage.childnodes[i].nodename; }  function adddiv() {     var = document.createelement("div");     stage.appendchild(a);     if (!stage.haschildnodes()) {         tt.innerhtml += stage.firstchild.nodename;     } else {         tt.innerhtml += stage.lastchild.nodename     } } 

jsfiddle

if want display divs during removal need change = += below

function remov() {     tt.innerhtml=''; //edit forgot add in line     if (stage.haschildnodes()) {         stage.removechild(stage.firstchild);         (var = 0; < stage.childnodes.length; i++) {             tt.innerhtml += stage.childnodes[i].nodename;  //<------- + needed display divs         }          if (!stage.haschildnodes()) {             tt.innerhtml = "no nodes";         }     } } 

edit fiddle added


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -