+ 1

removing all child node "p" from parent node "demo" by loop or else for exp.

I want to remove all child nodes "p" from parent node "demo" by using removeChilde(child) <div id="demo"> <p>This is a paragraph.</p> <p>This is another paragraph.</p> </div> var parent = document.getElementById("demo"); var isChild = parent.hasChildNodes(); var i = 0; while(isChild){ parent.removeChild(parent.childNodes); i++; }

3rd Jun 2020, 6:16 PM
M.ISMAIL
M.ISMAIL - avatar
3 Réponses
+ 1
so basically your point is like while(parent.firstChild){ parent.removeChild(parent.firstChild); } thanks.
3rd Jun 2020, 7:54 PM
M.ISMAIL
M.ISMAIL - avatar
+ 1
Yeah, that's how I would do it.
3rd Jun 2020, 10:10 PM
Russ
Russ - avatar
0
Try: parent.removeChild(parent.firstChild) instead.
3rd Jun 2020, 7:10 PM
Russ
Russ - avatar