I am trying to remove a the parent of a button, which is a list element, once the button has been clicked.
New to the programming world so it may be something simple, but I cant find a solutution based on what I have found with others posting on stack overflow and the internet. Please can someone point out what I am doing wrong?
I am using a combination of Javascript and Jquery. I am trying to utilise the skills I already know of plus a bit of researching in order to get the job done. I am in the process of learning the techniques and skills that would be required to have a similar remove event using pure Javascript, but for now I am just using Jquery for that side of things.
$(document).ready(function() {
document.getElementById("add").addEventListener("click", function() {
var taskinput = document.getElementById("task").value;
if (taskinput) {
var tasktext = document.createTextNode(taskinput);
var list = document.createElement("li");
list.appendChild(tasktext);
var button = document.createElement("button");
button.setAttribute("class", "completed");
button.innerHTML = "X";
list.appendChild(button);
document.getElementById("task-to-do").appendChild(list);
document.getElementById("task").value = " ";
} else {
alert("Please enter a task");
}
});
$("button .completed").click(function() {
$(this).closest("li").remove();;
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div id="container">
<header>
<h1>ENTER A TASK BELOW</h1>
<input type="text" id="task"><img id="add" src="add.png">
</header>
<div id="incomplete-tasks">
<h4>TO-DO LIST</h4>
<ul id="task-to-do">
</ul>
</div>