JavaScript-Code-for-To-do-List-knowledgecityfree
Code for generating To-do-List
Here is the best code for generating to-do-list. All HTML, CSS and JAVASCRIPT code are available here.
HTML CODE:
<div class="main">
<div class="row">
<input type="text" id="input-box">
<button class="addbutton" onclick="addTo()">Add</button>
</div>
<div>
<ul class="row1">
<!-- <li class="checked">Task-1</li>
<li>Task-2</li>
<li>Task-3</li>
<li>Task-4</li> -->
</ul>
</div>
</div>
CSS CODE:
body{
padding: 0;
margin: 0;
background-color: rgb(127, 246, 255);
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.main{
margin-top: 150px;
background-color: rgb(255, 255, 255);
padding: 15px;
}
.row input{
width: 400px;
height: 45px;
border-radius: 20px;
border: none;
outline: none;
/* position: relative; */
padding: 1px;
box-shadow: 1px 1px 3px gray;
font-size: 20px;
}
.row button{
width: 110px;
height: 40px;
border-radius: 20px;
background-color: rgb(253, 207, 0);
border: none;
/* position: absolute; */
/* right: 485px;
top: 169px; */
cursor: pointer;
}
row1{
display: flex;
position: relative;
}
.row1 li{
list-style: none;
font-size: 15px;
padding: 12px 8px 12px 40px;
cursor: pointer;
position: relative;
}
.row1 li::before{
content: '';
position: absolute;
height: 28px;
width: 28px;
border-radius: 50%;
background-image: url(images);
background-position: center;
background-size: cover;
border: 1px solid gray;
left:0px;
top: 5px;
}
.row1 li.checked{
color: #555;
}
.row1 li.checked::before{
background-image: url(checkmark-icon-2797531_1280.png);
}
.row1 li span{
position: absolute;
right: 10px;
font-size: 26px;
width: 30px;
height: 30px;
border-radius: 50%;
cursor: pointer;
text-align: center;
}
.row1 li span:hover{
background-color: #7e7d7d;
}
JAVASCRIPT CODE:
let row1 = document.querySelector('.row1');
let inputbox = document.querySelector('#input-box')
function addTo(){
if(inputbox.value === ''){
alert('Please write Something!')
}
else{
let li = document.createElement('li');
let span = document.createElement('span')
span.innerHTML = '\u00d7';
li.innerHTML = inputbox.value;
row1.appendChild(li);
li.appendChild(span);
inputbox.value = '';
saveData();
}
}
row1.addEventListener('click', function(e){
if(e.target.tagName === "LI"){
e.target.classList.toggle("checked");
saveData();
}
else if(e.target.tagName === "SPAN"){
e.target.parentElement.remove();
saveData();
}
}, false)
function saveData(){
localStorage.setItem('data', row1.innerHTML)
}
function showTask(){
row1.innerHTML = localStorage.getItem("data")
}
showTask();
Compelete Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>To-do-List</title>
<style>
body{
padding: 0;
margin: 0;
background-color: rgb(127, 246, 255);
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.main{
margin-top: 150px;
background-color: rgb(255, 255, 255);
padding: 15px;
}
.row input{
width: 400px;
height: 45px;
border-radius: 20px;
border: none;
outline: none;
/* position: relative; */
padding: 1px;
box-shadow: 1px 1px 3px gray;
font-size: 20px;
}
.row button{
width: 110px;
height: 40px;
border-radius: 20px;
background-color: rgb(253, 207, 0);
border: none;
/* position: absolute; */
/* right: 485px;
top: 169px; */
cursor: pointer;
}
row1{
display: flex;
position: relative;
}
.row1 li{
list-style: none;
font-size: 15px;
padding: 12px 8px 12px 40px;
cursor: pointer;
position: relative;
}
.row1 li::before{
content: '';
position: absolute;
height: 28px;
width: 28px;
border-radius: 50%;
background-image: url(images);
background-position: center;
background-size: cover;
border: 1px solid gray;
left:0px;
top: 5px;
}
.row1 li.checked{
color: #555;
}
.row1 li.checked::before{
background-image: url(checkmark-icon-2797531_1280.png);
}
.row1 li span{
position: absolute;
right: 10px;
font-size: 26px;
width: 30px;
height: 30px;
border-radius: 50%;
cursor: pointer;
text-align: center;
}
.row1 li span:hover{
background-color: #7e7d7d;
}
</style>
</head>
<body>
<div class="main">
<div class="row">
<input type="text" id="input-box">
<button class="addbutton" onclick="addTo()">Add</button>
</div>
<div>
<ul class="row1">
<!-- <li class="checked">Task-1</li>
<li>Task-2</li>
<li>Task-3</li>
<li>Task-4</li> -->
</ul>
</div>
</div>
<script>
let row1 = document.querySelector('.row1');
let inputbox = document.querySelector('#input-box')
function addTo(){
if(inputbox.value === ''){
alert('Please write Something!')
}
else{
let li = document.createElement('li');
let span = document.createElement('span')
span.innerHTML = '\u00d7';
li.innerHTML = inputbox.value;
row1.appendChild(li);
li.appendChild(span);
inputbox.value = '';
saveData();
}
}
row1.addEventListener('click', function(e){
if(e.target.tagName === "LI"){
e.target.classList.toggle("checked");
saveData();
}
else if(e.target.tagName === "SPAN"){
e.target.parentElement.remove();
saveData();
}
}, false)
function saveData(){
localStorage.setItem('data', row1.innerHTML)
}
function showTask(){
row1.innerHTML = localStorage.getItem("data")
}
showTask();
</script>
Post a Comment