let taskArray=[];
function deletetask(currentitem,button){
button.remove();
currentitem.remove();
}
function addTask(){
let task=document.getElementById('task').value;
if(task!= ''){
taskArray.push(task);
const ul=document.getElementById('list');
const li=document.createElement('li');
const button=document.createElement('input');
button.setAttribute('type','button');
button.setAttribute('value','Delete task');
button.setAttribute('onclick','deletetask(this.previousElementSibling,this)');
li.innerText=task;
ul.append(li);
ul.append(button);
li.classList.add('list-items');
}
else{
alert("Please Enter Task");
}
}
HTML FILE
<body>
<div class="container">
<h1>To-Do List</h1>
<form action="">
<input type="text" id="task" />
<input type="button" value="Add Task" onclick="addTask()" />
<ul id="list"></ul>
</form>
</div>
</body>