I need help figuring out how to create a function that can delete tasks from my todo list. Currently, each task has its own remove button, but I want to implement a single remove button at the bottom that removes checked or crossed out tasks. Any suggestions on how to achieve this?
function addTask(){
var text = $("#new-text").val();
if(text!=""){
$("#todolist").append('<li><input type="checkbox" class="done" />' + text + ' <button class="delete">Remove Task</button></li>');
$("#new-text").val('');
}
}
function deleteTask(){
if($(this).parent().css('textDecoration') == 'line-through' ) {
$(this).parent().remove();
}else{
$(this).parent().remove();
}
}
function markAsDone(){
if($(this).parent().css('textDecoration') == 'line-through' ) {
$(this).parent().css('textDecoration', 'none');
}else{
$(this).parent().css('textDecoration', 'line-through');
}
}
$(function() {
$('input[type=text]').keydown(function(e){
if(e.keyCode === 13){
addTask();
}
});
$("#add").on('click', addTask);
$(document).on('click', '.delete', deleteTask);
$(document).on('click', '.done', markAsDone);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1 id="x" class="position">To-Do List</h1>
<div contenteditable="true">
<ul id="todolist" class="background">
<li><input type="checkbox" class="done"/> Clean house <button class="delete">Remove Task</button></li>
<li><input type="checkbox" class="done"/>Buy milk <button class="delete">Remove Task</button></li>
</ul>
</div>
<input type="text" id="new-text" /><button id="add">Add</button>