JSFiddle - React, Tailwind, and code Playground
HTML
<body>
<div id="todo-header-box">
<h2>TODO</h2>
<input type="text" id="my-input" placeholder="Add new..."> <span id="new-btn">Add</span>
<br>
<br>
</div>
<div id="todo-body-box">
<ul id="my-todo-list">
</ul>
</div>
<h1 id="h"></h1>
<script src="script.js"></script>
</body>
CSS
* {
box-sizing: border-box;
}
html {
background: royalblue;
}
body {
width: 50%;
margin: auto;
}
ul {
margin: 0;
padding: 0;
list-style-type: none;
}
ul li {
cursor: pointer;
position: relative;
padding: 15px 10px 15px 40px;
text-align: left;
transition: 0.2s;
font-size: 20px;
color: #f5f5f5;
/* make the list items unselectable */
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
li:nth-child(even) {
background: #555;
}
li:nth-child(odd) {
background: #777;
}
#todo-header-box,
#todo-body-box {
width: 100%;
margin: 0 auto;
text-align: center;
}
#todo-body-box {
border: 2px solid #f5f5f5;
}
#todo-header-box {
border-right: 2px solid #f5f5f5;
border-left: 2px solid #f5f5f5;
border-top: 2px solid #f5f5f5;
padding: 10px;
background: #2d2d2d;
margin-top: 100px;
}
#my-input {
padding: 5px;
width: 80%;
font-size: 20px;
border: 2px solid #f5f5f5;
}
#new-btn {
background: #00a86b;
padding: 6px 20px;
color: #f5f5f5;
font-size: 23px;
position: relative;
right: 4px;
border: 1px solid #f5f5f5;
cursor: pointer;
}
#new-btn:hover {
background: rgba(0, 168, 107, 0.7);
}
h2 {
font-size: 30px;
color: #f5f5f5;
text-decoration: underline;
text-align: center;
}
li:hover {
background: #bbb;
}
ul li.checked {
text-decoration: line-through;
background: #000;
}
.closeBtn {
position: absolute;
top: 0;
right: 0;
padding: 15px 25px;
}
.closeBtn:hover {
background: #00a86b;
background: indianred;
}
JavaScript
/*
TODO REQUIRE:
Remove items from ToDoList - Remove the correct item
Why can't handle if empty
Make the code more Clean - Remove repeating code
*/
function getTodoItems() {
for (var i = 0; i < dataArray.length; i++) {
if (!dataArray[i].listItem.length) return;
var itemList = document.getElementById("my-todo-list");
var list = document.createElement("li");
itemList.appendChild(list);
list.innerHTML = dataArray[i].listItem;
var spanItem = document.createElement('span');
spanItem.style.float = 'right';
var myCloseSymbol = document.createTextNode('\u00D7');
spanItem.classList.add("closeBtn");
spanItem.appendChild(myCloseSymbol);
listItems[i].appendChild(spanItem);
close[i].onclick = function(e) {
var div = e.target.parentElement;
div.style.display = "none";
var position = dataArray.findIndex(item => item.listItem === div.childNodes[0].nodeValue);
dataArray.splice(position, 1); // Fix denne her linje
localStorage.setItem("itemListRecord", JSON.stringify(dataArray));
}
var list = document.getElementsByTagName('li');
list[i].onclick = function() {
this.classList.toggle("checked");
}
}
}
var keyAdd = false;
// Data Object
var dataArray = JSON.parse(localStorage.getItem("itemListRecord")) || new Array();
// Add Close Button to <li></li> element
var listItems = document.getElementsByTagName('li');
for (var i = 0; i < listItems.length; i++) {
var spanItem = document.createElement('span');
spanItem.style.float = 'right';
var myCloseSymbol = document.createTextNode('\u00D7');
spanItem.classList.add("closeBtn");
spanItem.appendChild(myCloseSymbol);
listItems[i].appendChild(spanItem);
}
// Remove <li></li> element from the list
var close = document.getElementsByClassName("closeBtn");
for (var i = 0; i <...