On clicking on add button in html page a div should be displayed on html page and multiple div should be shown on multiple clicks. Each div should have X icon and on clicking x, div should disappear from UI. Also it should maintain an array of id's of the dynamically generated div elements which should be shown on console and that id should be deleted whenever that particular div is removed.
This code is working fine but instead of using counter can i use array in which dynamically id will be generated whenever the div is created and id should be removed when div is deleted.?
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p>Click the button to make a Division element.</p>
<button id="button" onclick="myFunctionAdd()">Add</button>
<div id="myDIV">
</div>
<script>
var counter=1;
function myFunctionAdd()
{
var x = document.createElement("DIV");
x.id="div"
var divNumber = counter;
counter++;
console.log("Div Number: "+ divNumber + " is created");
var z= document.createElement("button");
z.id="btn";
x.setAttribute("style", "border :1px solid;height: 250px; width: 250px; top: 741px; left: 491px; padding:10px; margin: 50px;");
z.setAttribute("style", "background: #000 url(/home/subodh/Desktop/Widget/icon_incorrect.svg) no-repeat 0px 0px; height: 30px; width:40px; top: 6px; left: 4px; float: right; margin: 0px; padding:0px; clear: both; float:right;");
x.appendChild(z);
document.body.appendChild(x).appendChild(z);
z.onclick = function remove(btn)
{
console.log("Div Number: "+ divNumber + " is deleted");
x.parentElement.removeChild(x);
}
}
</script>
</body>
</html>