-2

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>
maggie
  • 85
  • 1
  • 2
  • 8

2 Answers2

5

See this example: https://codepen.io/dsomekh/pen/GEVNWb

function Clone() {


  var clone = document.getElementById('thediv').cloneNode(true); // "deep" clone
  document.getElementById("container").appendChild(clone);
}

function Delete(button) {
  var parent = button.parentNode;
  var grand_father = parent.parentNode;
  grand_father.removeChild(parent);
}
.mydiv {
  border: 2px solid red;
}
<div class="container" id="container">
  <div class="mydiv" id="thediv">
    I am the div
    <button type="button" onclick="Clone()" ) ">Clone</button>
      <button type="button " onclick="Delete(this) ")">Delete</button>
  </div>
  <div>
Bhargav Chudasama
  • 6,928
  • 5
  • 21
  • 39
David Somekh
  • 795
  • 3
  • 12
  • 33
  • Also in this code i want to show unique id of each div in console whenever they are created and that id should be removed whenever that particular div is removed. – maggie Jul 21 '17 at 05:08
1

try this one

function adddiv() {
  document.body.innerHTML += '<div id=' + Math.random() + '><button id="btn" onclick=closediv(this)>X</button>' + Math.random() + '</div>';
}

function closediv(e) {
  var par = $(event.target).parent();
  par.remove();
}
div {
  border: solid 1px red;
  width: 150px;
  height: 100px;
  margin: 2px;
}

#btn {
  clear: both;
  float: right;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="add" onclick=adddiv()>Add</button>

now all div created with unique id and using javascript

Bhargav Chudasama
  • 6,928
  • 5
  • 21
  • 39