-1

I have this script:

var text = ["Ground Beef", "Spaghetti", "Tomatoes", "Onions", "Cheese"];
text.forEach(function(el) {
    var div = document.createElement("p");
    div.className = "list";
    div.innerHTML = el;
    document.body.appendChild(div);
});

Please let me know how to add on click function to delete each for this elements Ground Beef ....

Mark
  • 1

1 Answers1

0

You could assign click event via onclick attribute. Then remove the element via e.target

var text = ["Ground Beef", "Spaghetti", "Tomatoes", "Onions", "Cheese"];
text.forEach(function(el) {
    var div = document.createElement("p");
    div.className = "list";
    div.innerHTML = el;
    div.onclick = remover
    document.body.appendChild(div);
});


function remover(e){
  e.target.remove()
}
prasanth
  • 22,145
  • 4
  • 29
  • 53