Untitled Document
Untitled Document
.container {
max-width: 600px;
margin: 0 auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 10px;
background-color: #f9f9f9;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
input[type="text"] {
width: 70%;
padding: 8px;
margin-right: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
button {
padding: 8px 16px;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #ddd;
}
ul {
list-style: none;
padding: 0;
}
li {
display: flex;
align-items: center;
justify-content: space-between;
padding: 8px 0;
border-bottom: 1px solid #ccc;
}
li input[type="checkbox"] {
margin-right: 10px;
}
.deleteTask {
background-color: #ff6f61;
color: #fff;
border: none;
border-radius: 5px;
}
.deleteTask:hover {
background-color: #ff403d;
}
```
function addTask() {
const taskText = taskInput.value.trim();
if (taskText !== '') {
const newTask = document.createElement('li');
newTask.innerHTML = `
<input type="checkbox">
<span>${taskText}</span>
<button class="deleteTask">Delete</button>
`;
taskList.appendChild(newTask);
tasks.push({ text: taskText, completed: false });
updateTaskCount();
taskInput.value = '';
}
}
addTaskButton.addEventListener('click', addTask);
taskList.addEventListener('click', function(event) {
const target = event.target;
if (target.classList.contains('deleteTask')) {
const index =
Array.from(target.parentNode.parentNode.children).indexOf(target.parentNode);
tasks.splice(index, 1);
target.parentNode.remove();
updateTaskCount();
} else if (target.type === 'checkbox') {
const index =
Array.from(target.parentNode.parentNode.children).indexOf(target.parentNode);
tasks[index].completed = target.checked;
updateTaskCount();
}
});
clearCompletedButton.addEventListener('click', function() {
tasks = tasks.filter(task => !task.completed);
const completedTasks = document.querySelectorAll('li input[type="checkbox"]:checked');
completedTasks.forEach(task => task.parentNode.remove());
updateTaskCount();
});
function updateTaskCount() {
taskCount.textContent = `Tasks: ${tasks.length}`;
}
```
After creating the files, open `index.html` in a web browser. You should see the interactive to-do
list application. You can add tasks, mark them as completed, delete them, and clear completed
tasks.