This is my first html project and I am trying to make a simple todo list. I have two buttons, an add and delete button. The add button works and displays whatever item I have the user enter into the input tag. Now I would like to remove any items from my list if they are checked and the user clicks the 'delete checked' button. I have some javascript written and this isn't working. Would anyone be able to point me in the correct direction on how to properly implement my delete button?
So in this screenshot, when the 'delete checked' button is pressed I would like for the 'eggs' item and 'oranges' item to be deleted.
<!DOCTYPE html>
<html>
<head>
<title>ToDoList</title>
<meta http-equiv="Content-Type" content="text/html" charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="apple-mobile-web-app-cabable" content="yes"/>
<style type="text/css">
h1 {
color: white;
text-align: center;
background-color: rgb(128,128,128);
font-weight: bold;
}
body {
font-family: helvetica, sans-serif;
font-size: 16px;
}
form {
text-align: center;
border-top: thin dotted;
padding-top: 1em;
border-bottom: thin dotted;
padding-bottom: 1em;
}
ul {
display: table;
margin: 0 auto;
}
input {
text-align: center;
width: wrap_content;
line-height: 1.4em;
border: 2px solid rgb(128,128,128);
border-radius: 5px;
padding: 10px 17px;
font-size: 14px;
}
button {
font-size: 14px;
border-radius: 5px;
background-color: rgb(128,128,128);
padding: 10px;
color: white;
font-weight: bold;
cursor: pointer;
}
.deleteall {
position: relative;
}
</style>
</head>
<body>
<head>
<h1>
ToDo-List
</h1>
</head>
<form>
<label>
Title:
<input id="newitem" type="text" placeholder="Tap to enter a new item…">
<br>
Details:
<input id="detail" type="text" placeholder="Details of new item…">
<br>
Date:
<input id="date" type="date" placeholder="year/month/day">
<br>
Time:
<input id="time" type="time" placeholder="00:00">
<br>
</label>
<button type="button" id="additem">
Add
</button>
<button class="button deleteall" type="button" id="deleteitems">
Delete Checked
</button>
</form>
<ul id="todo-list-container">
</ul>
<script type="text/template" id="item-template">
<li>
<label>
<input type="checkbox">
<!-- ITEM_NAME -->
</label>
</li>
</script>
<script>
var addNewItem = document.getElementById("additem");
var newItem = document.getElementById("newitem");
var toDoListContainer = document.getElementById("todo-list-container");
var itemTemplate = document.getElementById("item-template");
var template = itemTemplate.innerText;
var deleteAllItems = document.getElementById("deleteitems");
addNewItem.addEventListener('click', function(event) {
var itemName = newItem.value;
newItem.value = "";
var itemHTML = template.replace("<!-- ITEM_NAME -->", itemName);
toDoListContainer.insertAdjacentHTML('afterbegin', itemHTML);
});
deleteAllItems.addEventListener('click', function(event) {
var ul = document.getElementById("todo-list-container");
var items = ul.getElementsByTagName("li");
for (var i = 0; i < items.length; ++i){
if(items.checked == true){
// remove item from list
items.remove();
}
}
});
</script>
</body>
</html>