1 - I am adding Div container using JS but once their Counter crosses '5' i want to delete these Div containers ONE BY ONE from [0] index.
2 - Here below is what i have done so far.in which i am even getting the childNodes's Number but i am not able to delete these nodes..
var counter = 0;
function add_div() {
var mainDiv = document.getElementById("main-div");
var divi = document.createElement("DIV");
divi.setAttribute("ID", "div_id");
divi.style.backgroundColor = "red";
divi.style.height = "50px";
divi.style.width = "50px";
divi.style.margin = "20px";
mainDiv.appendChild(divi);
document.body.appendChild(mainDiv);
document.body.style.backgroundColor = "blue";
counter += 1;
document.getElementById("count").innerHTML = counter;
//read nodes
var ch = document.getElementById("main-div").childElementCount;
document.getElementById("demo").innerHTML = "number of childNodes: " + ch;
//counter check
if (counter > 5) {
mainDiv.removeChild(mainDiv.childNodes[0]);
// mainDiv.parentNode.removeChild(mainDiv);
}
}
<button onclick="add_div()">ADD DIV</button>
<div id="count"></div>
<div id="demo"></div>
<div id="main-div"></div>