Należy wykorzystać delegację
<div class="container">
<button id="add">add</button>
<div class="ractangle">
...
</div>
</div>
</div>
body {
background: #a53329;
}
button{
display: block;
margin: 0 auto;
width: 90px;
height: 30px;
background: #744253;
border: none;
color: #fff;
cursor: pointer;
}
button:hover{
background: #464258;
}
.container {
width: 106px;
min-height: 100px;
background: #b49286;
margin: 0 auto;
padding: 1.5px 0;
}
.square{
position: relative;
width: 100px;
height: 100px;
background-color:#d84336;
margin: 3px;
text-align: center;
line-height: 100px;
color: #fff;
cursor: pointer;
}
.square:hover:before{
content: "delete";
position: absolute;
width: 100px;
height: 100px;
background-color:#744253;
top: 0;
left: 0;
}
const addBtn = document.getElementById("add");
const ractangle = document.querySelector(".ractangle");
let item = 1;
addBtn.addEventListener("click",function(){
const el = document.createElement("div");
el.id = "punkt";
el.innerText = "Tekst " + item;
el.classList.add("square");
ractangle.appendChild(el);
item++;
});
ractangle.addEventListener("click",function(e){
e.target.remove();
item = 1;
});
metoda remove() nie jest wspierana przez IE :) Więc poszukaj innego rozwiązania.