JSFiddle - React, Tailwind, and code Playground
by Qwerty_Wasd
HTML
<div class="wrapper">
<div class="header">
<h1>ToDo List</h1>
<input type="text" placeholder="New item" id="toDoEl">
<span class="addBtn" onclick="newElement()">Add</span>
</div>
<ol id="list">
</ol>
</div>
<div id="demo">
<button id="btnFetch" type="button">Load More
</button>
</div>
CSS
* {
box-sizing: border-box ;
}
.wrapper {
width: 920 px;
margin: 0 auto;
}
ol{
margin: 0;
background-color: #eee;
}
ol li{
cursor: pointer;
position: relative;
padding: 12px 8px 12px 40px;
background-color: #eee;
font-size: 18px;
transition: 0.2s;
user-select: none;
}
ol li:nth-child(odd) {
background: f9f9f9;
}
ol li:hover{
background: #ddd;
}
ol li.checked{
background: #888;
color: #fff;
text-decoration: line-through;
}
ol li.chacked::before{
content: '';
position: absolute;
border-color: #fff;
border-style: solid;
border-width: 0 2px 2px 0;
top: 10px;
left: 16px;
transform: rotate(45deg);
width: 7px;
height: 15px;
}
.close{
position: absolute;
right: 0;
top: 0;
padding: 12px 16px;
}
.close:hover{
background: #f44336;
color: #fff;
}
.header{
background: #000;
padding: 30px 40px;
color: #fff;
text-align: center
}
.header:after{
content: "";
display: table;
clear: both;
}
input{
border: none;
width: 75%;
padding: 10px;
float: left;
font-size: 16px;
}
.addBtn {
padding: 10px;
width: 25%;
background: #d9d9d9;
color: #555;
float: left;
text-align: center;
font-size: 16px;
cursor: pointer;
transition: 0.3s;
}
.addBtn:hover{
background-color: #bbb;
}
.edit{
position: absolute;
right: 50px;
top: 0;
padding: 12px 16px;
}
.edit:hover{
background: #f44336;
color: #fff;
}
JavaScript
var list = document.querySelector('ol');
var todos;
function toLocal(){
todos = list.innerHTML;
localStorage.setItem('todos', todos);
}
list.addEventListener('click', function(ev){
if(ev.target.tagName === 'LI') {
ev.target.classList.toggle('checked');
toLocal();
}
else if(ev.target.className === 'close'){
var div = ev.target.parentNode;
div.remove();
toLocal();
}
}, false);
function newElement(){
var li = document.createElement('li');
var inputValue = document.getElementById('toDoEl',).value;
var t = document.createTextNode(inputValue);
li.appendChild(t);
if(inputValue ==""){
alert("Input your todo");
}
else{
document.getElementById('list').appendChild(li);
}
document.getElementById('toDoEl').value = "";
var span = document.createElement('close');
var txt = document.createTextNode("X");
var red = document.createElement('edit');
var edit = document.createTextNode("Edit");
span.className = 'close';
red.className = 'edit'
red.appendChild(edit);
li.appendChild(red);
span.appendChild(txt);
li.appendChild(span);
toLocal();
}
if(localStorage.getItem('todos')) {
list.innerHTML = localStorage.getItem('todos');
}
var btn = document.getElementById('btnFetch')
var liContainer = document.getElementById("list")
btn.addEventListener("click", function(){
var ourReqest = new XMLHttpRequest();
ourReqest.open('GET', 'https://jsonplaceholder.typicode.com/todos');
ourReqest.onload = function() {
var ourData = JSON.parse(ourReqest.responseText);
renderHTML(ourData);
};
ourReqest.send();
});
function renderHTML(data) {
var htmlString ="";
var arrItems = data.slice(0, 10);
arrItems.forEach(function(element) {
htmlString += "<li>" + element.title + "<span class='edit'>Edit</span><span class='close'>X</span></li>";
});
...