Please help to configure the button on removing dynamic elements. I have an code : https://www.w3schools.com/code/tryit.asp?filename=G2T2WSPSDUVS
Code :
<!DOCTYPE html>
<html>
<body>
<div>
<input type="checkbox" id="coffee" name="coffee" checked>
<label for="coffee">Coffee</label>
</div>
<div>
<input type="checkbox" id="gym" name="gym">
<label for="gym">Gym</label>
</div>
<div>
<input type="checkbox" id="rose" name="rose">
<label for="rose">Rose</label>
</div>
<button onclick="myFunction()">Try it</button>
<ul id="myList"></ul>
<script>
function myFunction() {
var node = document.createElement("LI");
var checkBoxCoffe = document.getElementById("coffee");
var checkBoxGym = document.getElementById("gym");
var checkBoxRose = document.getElementById("rose");
var textnode = document.createTextNode("");
if (checkBoxCoffe.checked == true){
textnode.textContent=textnode.textContent+"Coffee; "
}
if (checkBoxGym.checked == true){
textnode.textContent=textnode.textContent+"Gym; "
}
if (checkBoxRose.checked == true){
textnode.textContent=textnode.textContent+"Rose; "
}
var button = document.createElement("button");
button.innerHTML = "Remove";
node.appendChild(textnode);
node.appendChild(button);
document.getElementById("myList").appendChild(node);
}
</script>
</body>
</html>
How I can do that each button will remove exectly selected li element? everything is working only remove button still need to do thanks